问题
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