0

Good day!

I'm using ASP.NET MVC 2 and T4MVC and it seems some code magic is happening

When I add this to routes table:

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

How does framework know that I want this rule to apply when I write something like this in the view to generate outgoing URL:

<%: Url.Action(MVC.Profile.Login() %>

What if I have multiple different rules (with different params) for the same controller/action pair? Which one will be chosen? Is there anywhere a good description of this behavior?

Thanks in advance!

artvolk
  • 9,448
  • 11
  • 56
  • 85

3 Answers3

1

It matches the route patterns in the order you define them. Thats why you have the default pattern as the last one. As soon as it finds a matching pattern, it stops looking.

Edit

Parameters are ignored during route matching. Once a controller method has been selected, mvc uses model binding to assign the parameters to the method variables.

If you can explain what type of url structure you are looking to use, we can probably help you more.

mdm20
  • 4,475
  • 2
  • 22
  • 24
1

What I would suggest to help you understand how this work is to separate the magic that T4MVC does from what MVC itself does under the cover.

When you write this with T4MVC:

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

It's equivalent to writing this with straight MVC:

routes.MapRoute(
    "Login",
    "login/",
    new { controller = "Profile", action = "Login" }
);

And in the view:

Url.Action(MVC.Profile.Login())

Is the same as

Url.Action("Login", "Profile")

T4MVC gives you the benefit of strong typing/intellisense, but in the end what it does is the same as with straight MVC.

Hopefully this helps clear things up a bit :)

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Thanks for the quick response. So if I have two routes to the same controller, say -- one with paging parameter in form `/view/page-2` and one without `/view/` I need to specify route name to generate the URL using route that comes second in `Global.asax`. As far as I recall there is no strongly-typed helpers for route names in T4MVC? – artvolk Mar 23 '11 at 07:20
  • 1
    You are correct that T4MVC does not provide strongly typed helpers for route names. But you can get strong typing simply by defining public constants that you use both in your route declarations and in your views. – David Ebbo Mar 23 '11 at 19:20
  • Could you please explain more on how I can get the strong typing to work. I'm using MvcContrib Portable Area and cannot get T4MVC to work because views/controllers return without the additional "Areas/MyArea" in the path. – Tri Q Tran May 24 '11 at 03:47
  • @TriQ: might make sense to start a new question as I'd need to see more details about what you're doing. – David Ebbo May 26 '11 at 17:36
-1

Your example is not valid MVC, you would normally pass the controller name, action and any other parameters, then the routing engine would use all that information to determine which route to use, the more routes you have defined, the more information it will probably require to determine the one YOU want to match

BlackTigerX
  • 6,006
  • 7
  • 38
  • 48
  • The method accepts an object, so technically anything is valid. If that MVC.Profile.Login() method returned a class that had controller and action properties, it would work fine, wouldn't it? – mdm20 Mar 22 '11 at 20:43
  • The call is indeed quite correct, and is the way T4MVC calls always look like. – David Ebbo Mar 22 '11 at 23:40