本文介绍了MVC区域-非区域路线解析为区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在MVC 3项目中添加了一个区域.我似乎无法在非常简单的情况下使用路由.似乎总是想解决该问题.这是我的配置.在启动时:

I have added an area to my MVC 3 project. I cannot seem to get routing working with a very simple scenario. It seems to always want to resolve to the area. Here is my configuration. At startup:

AreaRegistration.RegisterAllAreas();
IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Browse", action = "Index", id = UrlParameter.Optional }

还有

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Admin"; }
    }

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

在web.config中:

In web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Login" defaultUrl="~/Browse" timeout="60" cookieless="UseDeviceProfile" />
</authentication>

我正在使用RouteDebugger尝试解决它.当我导航到登录"页面时,调试器将显示:

I am using RouteDebugger to try to solve it. When I navigate to the Login page the debugger shows:

  • AppRelativeCurrentExecutionFilePath:〜登录
  • 管理员/{控制器}/{操作}/{id} 不匹配当前请求
  • {controller}/{action}/{id} 匹配当前请求
  • 匹配的路线:{controller}/{action}/{id}
  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} Does Not Match Current Request
  • {controller}/{action}/{id} Matches Current Request
  • Matched Route: {controller}/{action}/{id}

到目前为止,一切都很好.但随后它显示了这一点:

So far so good. But then it shows this:

  • 生成的URL:/Admin/Login?ReturnUrl =%2F,使用路由"Admin/{controller}/{action}/{id}"

下一步,我登录.我的Login/Index方法未命中,调试器显示:

Next I log in. My Login/Index method is not hit, and the debugger shows:

  • AppRelativeCurrentExecutionFilePath:〜登录
  • 管理员/{控制器}/{操作}/{id} 不匹配当前请求
  • {controller}/{action}/{id} 匹配当前请求
  • 匹配的路线:{controller}/{action}/{id}
  • 生成的URL:/Admin/Login?ReturnUrl =%2FAdmin%2FLog使用路由"Admin/{controller}/{action}/{id}"
  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} Does Not Match Current Request
  • {controller}/{action}/{id} Matches Current Request
  • Matched Route: {controller}/{action}/{id}
  • Generated URL: /Admin/Login?ReturnUrl=%2FAdmin%2FLogin using the route "Admin/{controller}/{action}/{id}"

一方面,它说它与Admin路由不匹配,然后在生成的URL中说它正在使用该路由.我很困惑.

On the one hand it says that it does not match the Admin route, then in the generated URL it says it's using that route. I'm stumped.

推荐答案

尝试将具有预定义值的area参数添加到路由定义中...例如,代替:

Try to add your area parameter with a predefined value to your routing definition... For example instead of:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Users", action = "Index", id = UrlParameter.Optional }
        );

使用:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { area = "Admin", controller = "Users", action = "Index", id = UrlParameter.Optional }
        );

让我知道是否有帮助...问候

Let me know if it helps...Regards

这篇关于MVC区域-非区域路线解析为区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:17