加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 20:52:51 所属栏目:asp.Net 来源:网络整理
导读:我正在使用MVC3并在我的应用程序中有区域.一般来说,一切正常,我可以导航到我的区域(例如Admin = Area,Controller = Department),如下所示: %: Html.ActionLink("Departments","DepartmentIndex","Department",new { area = "Admin"},null )% 但是,我注意到
我正在使用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 { })

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读