Why is unmodified template code in my MVC4 app trying to register areas twice?

核能气质少年 提交于 2019-12-13 06:48:25

问题


I have a new MVC4 app that I create off the MVC4 internet project template in VS11 beta. Now when I deploy the app to my web site hosting provider, and I leave the following line active in my Global.asax class, I get an error about MVC trying to register a route that already exists. When I comment out this line, everything runs fine, but action links to area specific controllers are broken, as I ask about in this question.

AreaRegistration.RegisterAllAreas();

回答1:


If you have an old dll in your deployment bin directory (if you renamed your project or dlls at some point) then you'd get this error.

Make sure the bin directory is getting cleaned when you deploy.




回答2:


This usually happens when you have a controller in an area with the same name as a controller in your root website. i.e. "WebsiteNamespace.Controllers.HomeController" and "WebsiteNamespace.Areas.MyArea.Controllers.HomeController".

To rectify this, you need to declare namespaces on your base website routes:

        routes.MapRoute("Default", "{controller}/{action}/{id}", new {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        }, new[] { "WebsiteNamespace.Controllers" });


来源:https://stackoverflow.com/questions/11535369/why-is-unmodified-template-code-in-my-mvc4-app-trying-to-register-areas-twice

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