微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-3 – 带区域的MVC – Html.ActionLink返回错误的URL /路由?

我正在使用MVC3并在我的应用程序中有区域.一般来说,一切正常,我可以导航到我的区域(例如Admin = Area,Controller = Department),如下所示:
<%: Html.ActionLink("Departments","DepartmentIndex","Department",new { area = "Admin"},null )%>

但是,我注意到的是,如果我没有在ActionLink中指定区域,例如

<%: Html.ActionLink("Test","Index","Home")%>

如果我已导航到“管理员”区域,这将停止工作.即我的网址现在
http://localhost/myproj/Admin/Home/Index
代替
http://localhost/myproj/Home/Index

关于这里发生了什么的任何想法?

创建MVC应用程序/区域时,我的路由是所有认值.即

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",// Route name
            "{controller}/{action}/{id}",// URL with parameters
            new { controller = "Home",action = "Index",id = UrlParameter.Optional },new[] { "MyProj.Controllers" } 
        );
    }

我的区域注册

public override void Registerarea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default","Admin/{controller}/{action}/{id}",new { action = "Index",id = UrlParameter.Optional }
        );
    }

这是设计的吗?
该帖子建议使用RouteLink而不是ActionLink:
说使用RouteLink
Is it required to have “area” routevalue on actionlink if the application was grouped into areas?

解决方法

This StackOverflow answer正确地指出,如果您正在使用区域,则需要提供区域名称.

例如.

Html.ActionLink("Home","Home",new { Area = "" },new { })

原文地址:https://www.jb51.cc/aspnet/245426.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐