Does ASP.NET MVC create default routes for areas

牧云@^-^@ 提交于 2019-12-06 04:10:26

问题


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?


回答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.

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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/4861702/does-asp-net-mvc-create-default-routes-for-areas

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