0

Why does:

<%= Html.ActionLinkForAreas<UsersController>(c => c.User(), "My Details") %>

Generate an URL containing this:

Users/User

But:

<%= Html.ActionLinkForAreas<BlaController>(c => c.Index(1), "My Bla Di Bla")%>

An URL like this:

Bla

Rather than this:

Bla/Index

In other words why is the Index action ‘swallowed’. Does this have to do with the routing which looks like this:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

routes.RouteExistingFiles = true;
routes.IgnoreRoute("Content/{*wildcard}");
routes.IgnoreRoute("Scripts/{*wildcard}");

routes.MapRoute(
"Default",                                              // Route name
"{controller}/{action}/{id}",                           // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
);

Or is there another reason? How can I change this behaviour? Thanks.

Best wishes,

Christian

1 Answer 1

3

Becaues you've specified Index as your default action:

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
);

The action = "Index" part. Since it's the default action, whenever your create URLs to it, the "Index" part will be omitted. This affords you the opportunity to have nice concise URLs. Incidentally, the same rule applies to the controller itself. If you route to the "Home" controller, URLs to it will have the "Home" part elided, thus allowing you to have a raw base URL such as "/".

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I am asking this because my mvc contrib grid swallows up the index bit. see also: stackoverflow.com/questions/4452031/mvc-contrib-pager - 3rd comment to my question. I guess one option would be to redirect to the index action and use some other action than Index for my actionlinks etc.
@csetzkorn, not sure if there's some idiosynchracy with the pager. But you certainly don't have to use default actions. You can happily remove the action = "Index" part and your site will work just fine. Just some of your URLs will be longer than they would otherwise because they will always include the action name.
Alternatively could I add an exception for one controller (to use the long Index URL)? Thanks!
Yes, you could add an additional route with the specific controller you have in mind hard-coded (instead of {controller} use YourControllerName) and make sure this new route appears before your other routes. For this particular route, don't include any defaults for action, but do include a default for your controller name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.