ASP.NET MVC Area not picking up the correct route

落爺英雄遲暮 提交于 2019-12-11 16:45:08

问题


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.

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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4855574/asp-net-mvc-area-not-picking-up-the-correct-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!