How to enable gracefull fallback to "root" controllers if controller is not found in area
I have an ASP.NET MVC3 Razor application getting too big to manage effectively. To have better control over the code I implemented Areas and re-arranged the code to be in the respective areas.
Things that are used from multiple areas are left in the "norma开发者_运维问答l" controller folder.
My problem arises as soon as I use an Action or ActionLink from an area. I have to manually include a routing parameter called 'area = ""' to have the link work.
There are a lot of links to be changed, so I tried to have the controller selection fallback to the "root" controllers to find the needed controller. I had no luck so far.
What do I have to do to enable MVC3 to search through the area controllers and continue to search in the root controllers folder if the controller is not found?
Thanks Andreas
You can do this by setting up your routes appropriately.
Each area has it's own route supplier, and then there are the routes in the global.ascx. The routes act sort of like a case statement, where it tries to find the routes in the areas first, and if it doesn't find a matching route, it then falls through to the global.ascx routes.
So, in your new areas, setup a catchall route, and in the global.ascx setup a catchall route and you should be ok.
You can use Haack's route debugger to see what is happening with your routes.
精彩评论