ASP.NET MVC `Html.ActionLink` between “Areas”

后端 未结 6 679
灰色年华
灰色年华 2021-01-31 01:58

I have added a new Area to my MVC3 project and I am trying to link from the _Layout page to the new Area. I have added an Area called \'Admin\' that has a controller \'Meets\'.<

6条回答
  •  情书的邮戳
    2021-01-31 02:38

    Another option is to utilize RouteLink() instead of ActionLink(), which bypasses the area registrations altogether:

    ActionLink version:

    Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, null)
    

    RouteLink version:

    Html.RouteLink("Log Off", "Default", 
        new { action = "LogOff", controller = "Account" })
    

    The second parameter is a "Route Name" which is registered in Global.asax.cs and in various 'AreaRegistration' subclasses. To use 'RouteLink' to link between different areas, you only need to specify the correct route name.

    This following example shows how I would generate three links to different areas from a shared partial, which works correctly regardless of which area I am 'in' (if any):

    @Html.RouteLink("Blog", "Blog_default", 
        new { action = "Index", controller = "Article" })
    
    @Html.RouteLink("Downloads", "Download_default", new { action = "Index", controller = "Download" })
    @Html.RouteLink("About", "Default", new { action = "Index", controller = "About" })

    Happy coding!

提交回复
热议问题