How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

后端 未结 4 956
野的像风
野的像风 2021-02-01 01:30

I recently discovered Areas in ASP.NET MVC 4, which I have successfully implemented, but I\'m running into troubles with the @Html.ActionLink helper in my Razor vie

4条回答
  •  青春惊慌失措
    2021-02-01 02:17

    I hate answering my own question, but @Matt Bodily put me on the right track.

    The @Html.Action method actually invokes a controller and renders the view, so that wouldn't work to create a snippet of HTML in my case, as this was causing a recursive function call resulting in a StackOverflowException. The @Url.Action(action, controller, { area = "abc" }) does indeed return the URL, but I finally discovered an overload of Html.ActionLink that provided a better solution for my case:

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

    Note: , null is significant in this case, to match the right signature.

    Documentation: @Html.ActionLink (LinkExtensions.ActionLink)

    Documentation for this particular overload:

    LinkExtensions.ActionLink(Controller, Action, Text, RouteArgs, HtmlAttributes)

    It's been difficult to find documentation for these helpers. I tend to search for "Html.ActionLink" when I probably should have searched for "LinkExtensions.ActionLink", if that helps anyone in the future.

    Still marking Matt's response as the answer.

    Edit: Found yet another HTML helper to solve this:

    @Html.RouteLink("Admin", new { action = "Index", controller = "Home", area = "Admin" })
    

提交回复
热议问题