开发者

Why is @Html.ActionLink leaving off the controller and action portions of the link?

开发者 https://www.devze.com 2023-03-14 17:42 出处:网络
I am creating a link inside the main _Layout.cshtml file inside a MVC3 application. @Html.ActionLink(\"Security\", \"Index\", \"Home\", new { area = \"Security\" }, new { })

I am creating a link inside the main _Layout.cshtml file inside a MVC3 application.

@Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { })

When the page is rendered, this is the resultant Html

<a href="/Security">Security</a>

and if I click on this link, I get a blank page.

I have routedebugger installed and here are the results:

Why is @Html.ActionLink leaving off the controller and action portions of the link?

I have the default route inside the Global.ascx.cs as follows:

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

and the Security Area's route:

context.MapRoute(
            "Security_default",
            "Security/{controller}/{action}/{id}",
            new { area = "Security", controller = "Ho开发者_StackOverflow社区me", action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
        );       

If I directly type in the following URL, http://localhost:1410/Security/home, the correct page is presented.

How can I get the @Html.Action link to include the controller portion of the URL?

I do have a Home controller with an Index action inside the root portion of the application and had to include the Namespace filters to the route registrations.

Thank you, Keith


Change the Security Area's route mapping to the following:

context.MapRoute(
        "Security_default",
        "Security/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
    );    

Notice the area and controller portions of the defaults parameter were removed.

Now the @Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { }) renders <a href="/Security/Home">Security</a>

Keith

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号