2
votes

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?

2
bcoz it is default routeNilesh Gajare
@Nilesh : but in the code in _layout.cshtml, where did we ever indicate such a thing. There's no difference in the above 3 links, and we we not indicating in any way that the link for home is defaultuser3547820
@Nilesh: 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?user3547820
@user3547820 when suitable route found, in your case it route Default, mvc checks which params come in and if some parameters is absent then using parameters from default. no redirectGrundy
@Grundy please see edits and comments to other answersuser3547820

2 Answers

2
votes

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

0
votes

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.