ASP.NET MVC 区域未选择正确的路线

发布于 2024-10-15 04:26:34 字数 1726 浏览 2 评论 0原文

我正在调试 MVC 3 应用程序上的路由问题,并且正在使用 Phil Hacks 路由调试器。

我似乎无法弄清楚下面以黄色突出显示的路线的起点。每次我使用以下请求运行应用程序

http://www.mywebsite.com/ auth/login?ReturnUrl=/

这条路由首先出现,然后给我一个 404 错误,因为我没有索引操作。如您所见,我已将默认路由设置为使用登录操作方法,但该路由仍然存在。

在此处输入图像描述

我有以下路由配置:

AuthAreaRegistration

public class AuthAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Auth";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "login",
            "auth/login/{*returnPath}",
            new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
        );

        context.MapRoute(
            "Auth_default",
            "Auth/{controller}/{action}/{id}",
            new { controller = "Auth", action = "LogIn", id = "" }
        );
    }
}

Global.asax(使用 T4 MVC 模板)

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

        routes.MapRoute(
            "Home",
            "{controller}/{action}/{id}",
            MVC.Home.Index(), new { id = UrlParameter.Optional },
            new string[] { "MyNamespace.WebUI.Controllers" }

        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            MVC.Home.Index(), new { id = UrlParameter.Optional },
            new string[] { "MyNamespace.WebUI.Controllers" }
        );
    }

I am in the process of debugging a routing issue on my MVC 3 application and I am using Phil Hacks routing debugger.

I cannot seem to work out where the route highlighted in yellow below is originating. Each time I run my application with the following request

http://www.mywebsite.com/auth/login?ReturnUrl=/

this route comes first and then gives me a 404 error as I do not have an index action. As you can see I have set my default routes to use the Login action method but still this route persists.

enter image description here

I have the following route configurations:

AuthAreaRegistration

public class AuthAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Auth";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "login",
            "auth/login/{*returnPath}",
            new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
        );

        context.MapRoute(
            "Auth_default",
            "Auth/{controller}/{action}/{id}",
            new { controller = "Auth", action = "LogIn", id = "" }
        );
    }
}

Global.asax (Using T4 MVC Templates)

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

        routes.MapRoute(
            "Home",
            "{controller}/{action}/{id}",
            MVC.Home.Index(), new { id = UrlParameter.Optional },
            new string[] { "MyNamespace.WebUI.Controllers" }

        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            MVC.Home.Index(), new { id = UrlParameter.Optional },
            new string[] { "MyNamespace.WebUI.Controllers" }
        );
    }

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

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

发布评论

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

评论(2

小糖芽 2024-10-22 04:26:35

我不想回答我自己的问题,但经过一天的尝试解决这个问题后,我想我会发布答案,以防其他人遇到同样的问题。

事实证明,我的应用程序保留了旧路线并将它们填充到我的路线集合中。我删除了 bin 文件夹中的所有文件并重建了我的解决方案,一切正常。

我在这里更详细地回答了这个问题:

Does ASP .NET MVC为区域创建默认路由

I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.

It turns out that my application was holding onto old routes and populating them into my route collection. I deleted all the files in my bin folder and rebuilt my solution and everything worked as it should.

I have answered this question in a little more detail here:

Does ASP.NET MVC create default routes for areas

ˉ厌 2024-10-22 04:26:35

我认为问题在于,您在外部有一个名为 Auth区域和一个名为 Auth控制器的地区。

MVC 会首先尝试将您的 url 与身份验证区域进行匹配,但您实际上希望它到达区域之外的身份验证控制器。

解决这个问题的最好方法是避免控制器/区域的名称不明确。

I think the problem is in the fact that you have an area called Auth and a controller called Auth outside of the areas.

MVC will try to match your url against Auth area first but you actually want it to hit your auth controller outside an area.

The best way to solve it imho is to avoid a ambiguous names of controllers/areas.

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