Does ASP.NET MVC create default routes for areas

2019-04-10 09:07发布

I have a couple of areas in my MVC 3 application Auth and Users. I am using Phil Haacks Route Debugging tool to view a list of my routes and see which one gets selected based on my url.

However there are a couple of routes present that I have not created in either my AreaRegistration file or Globalasax and I don’t know where they have come from or how to get rid of them. The routes are highlighted in yellow below.

You can also see that I have created a default route in my Auth area (highlighted in green) which simply points to the Login action of my Auth controller. I have debugged the RouteTable and it gets added when the AreaRegistration.RegisterAllAreas(); method is called. However it does not get added in the AreaRegistration as have stepped through this also.

Does ASP.NET MVC add this as a default and if so can I remove it somehow?

enter image description here

3条回答
Root(大扎)
2楼-- · 2019-04-10 09:24

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.

In the end I got rid of all my areas from my application and just had the basic Global.asax routing. When I ran the app I could see in the route debugger that the routing collection was still being populated with the routes from now non existing areas. After trying many things including deleting everything from my ASP.NET temp files, messing around with IIS AppPools and cleaning out browser data I finally came across the answer.

I deleted everything from the websites bin folder, did a rebuild and low and behold, the routes were gone. I reinstated my areas with the config described and everything is working as it should.

I have no idea why my MVC app was holding onto and populating the old routes but as soon as my bin was cleared and new dll’s created everything worked as it should. If anybody out there knows why this may be then I would be very interested.

查看更多
叛逆
3楼-- · 2019-04-10 09:28

Yes, each area has it's own AreaRegistration file that defines area routes. Look for it in your area root folder.

For your User area, look in Areas -> User -> UserAreaRegistration.cs

It should contain something like this:

    public class UserAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "User";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "User_default",
                "User/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
查看更多
你好瞎i
4楼-- · 2019-04-10 09:46

Did you rename your project? It loads the routes by reflection, probably by scanning everything in the bin folder. So if you refactored your code and changed the assembly name, you could have easily had old code being picked up and registering those routes.

JB

查看更多
登录 后发表回答