cs0815
cs0815

Reputation: 17388

ASP.NET MVC routing problem?

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

Upvotes: 0

Views: 213

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77536

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 "/".

Upvotes: 3

Related Questions