ASP.NET MVC Area not picking up the correct route

2019-09-07 02: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.

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

2条回答
时光不老,我们不散
2楼-- · 2019-09-07 02:15

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.

查看更多
叼着烟拽天下
3楼-- · 2019-09-07 02:37

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

查看更多
登录 后发表回答