MVC与地区 - Html.ActionLink返回错误的URL /路由? [英] MVC with Areas -- Html.ActionLink returning wrong URL/Route?

查看:350
本文介绍了MVC与地区 - Html.ActionLink返回错误的URL /路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3,并在我的应用领域。一般情况下,一切工作正常,我可以浏览到我的区域(例如管理员=面积,控制器=部门)是这样的:

I am using MVC3 and have Areas in my application. In general, everything works fine, I can navigate to my area (e.g. Admin=Area, Controller=Department) like this:

<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>

不过,我注意到的是,如果我不我ActionLink的指定区域,例如

However, what I noticed is that if I don't specify the area in my ActionLink, e.g.

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

这将停止工作,如果我导航到管理区域。即我的网址是现在
        的http://本地主机/ MyProj文/管理/首页/索引
代替
        的http://本地主机/ MyProj文/首页/索引

This will stop working if I have navigated to the "Admin" area. i.e. my url is now http://localhost/myproj/Admin/Home/Index instead of http://localhost/myproj/Home/Index

什么是怎么回事任何想法?

我的路线都创建一个MVC应用程序/区时的默认设置。即。

My routes are all the defaults when creating an MVC app / Areas. i.e.

    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
<一href=\"http://stackoverflow.com/questions/5549497/is-it-required-to-have-area-routevalue-on-actionlink-if-the-application-was-gro\">Is区&QUOT;它有&QUOT必需的;在ActionLink的routevalue如果应用程序分为领域?

推荐答案

<一个href=\"http://stackoverflow.com/questions/5549497/is-it-required-to-have-area-routevalue-on-actionlink-if-the-application-was-gr\">This StackOverflow的正确回答指出您需要的,如果你使用领域提供了名称。

This StackOverflow answer correctly states that you need to provide the Area name if you're using areas.

如:

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

这篇关于MVC与地区 - Html.ActionLink返回错误的URL /路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆