MVC 区域 - 非区域路由解析为区域

发布于 2024-11-02 02:47:27 字数 1863 浏览 2 评论 0原文

我已在我的 MVC 3 项目中添加了一个区域。我似乎无法在非常简单的场景中使用路由。看来总是想去区解决一下。这是我的配置。启动时:

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 中:

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

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

  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} 不匹配 当前请求
  • {controller}/{action}/{id } 匹配 当前请求
  • 匹配的路由:{controller}/{action}/{id}

到目前为止一切顺利。但随后它显示:

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

接下来我登录。我的 Login/Index 方法未命中,并且调试器显示:

  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} Does Not Match Current Request
  • {controller}/{action}/{id} 匹配 当前请求
  • 匹配路由:{controller}/{action}/{id}
  • 生成的 URL:/Admin/Login?ReturnUrl=%2FAdmin%2F使用路由“Admin/{controller}/{action”登录}/{id}"

一方面,它表示它与管理路由不匹配,然后在生成的 URL 中,它表示它正在使用该路由。我很困惑。

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 }

And

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 }
        );
    }
}

In web.config:

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

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

  • 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:

  • Generated URL: /Admin/Login?ReturnUrl=%2F using the route "Admin/{controller}/{action}/{id}"

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

  • 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}"

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-11-09 02:47:27

尝试将具有预定义值的区域参数添加到路由定义中...例如,而不是:

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 }
        );

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

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 }
        );

use:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文