Reputation:
I created a default ASP .NET MVC 4 Internet Application in Visual Studio.
In the _Layout.cshtml, for the case of three action links of Home, About and Contact, the home link goes to http://localhost
, but the other two go to the respective action and controller ((http://localhost/home/about
) and (http://localhost/home/contact
)).
Why such difference occurs. Just because index action of home controller is default, does compiler think that html.action link for home will go to http://localhost
and not http://localhost/home/index
. ?
Here is the code in _Layout.cshtml
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
The code in routeconfig.cs is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Edit
I understand that http://localhost
will redirect to index action of home controller but why is the html.actionlink giving the link as http://localhost
and not http://localhost/home/index
. ?
Edit 2
This defaults in routeconfig would imply that in the event that the url is simply localhost, only then the default action and controller is to be taken. When routeconfig would be executed, the url for parsing is localhost. My question is why isn't it localhost/home/index, since in actionlink we're specifying the name of controller and action? Is the compiler so intelligent that the url for localhost/home/index is simply converted to localhost?
I know that localhost is redirected to index action of home controller, but my question is that in the home page as specified by the _Layout.cshtml, why is the url for home link different than that of about and contacts?
Upvotes: 2
Views: 1655
Reputation: 11975
Its probably because of this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
What you pass for defaults
...
Upvotes: 2
Reputation: 62488
you are setting this route default, so it will be your application start url, like index.html in html pages.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Here you are telling that its a default route so in this case it will not display action and controller name in the url.
Upvotes: 0