问题
I'm trying to get ASP.NET MVC 2 single-project area registration to work. Tried with Preview 2 and now with Beta version with no luck. I used the "Add area" dialog to create a "NewsModule" area. Created a NewsModuleController inside it and an Index view for it.
The route registration for this Area looks like this:
context.MapRoute( "NewsModule_default", "NewsModule/{action}/{id}", new { action = "Index", id = "", controller = "NewsModule", area = "NewsModule" } );
I added the AreaRegistration.RegisterAllAreas(); call to my Global.asax. Accessing http://localhost/mymvcproj/NewsModule gets a HTTP 404 error.
Using Phil Haack's route debugger, I could confirm that the route is being correctly mapped and catched by this URL, however, it seems like the framework is not being able to locate the Area files, maybe?
Anyone can help?
Thanks, Felipe
回答1:
Problem solved. Here is what I did to solve:
AreaRegistration.cs file:
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "NewsModule_default", "NewsModule/{controller}/{action}/{id}", new { controller = "NewsModule", action = "Index", id = "" }); }
IMPORTANT: Add the ".Areas." to the namespace (Namespace.Areas.ControllerName).
Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // must be called RIGHT AFTER IgnoreRoute() AreaRegistration.RegisterAllAreas(); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); }
回答2:
Try removing "area = "NewsModule" from the route registration in the Area/NewsModule folder. So it appears like,
context.MapRoute(
"NewsModule_default",
"NewsModule/{action}/{id}",
new { action = "Index", id = "", controller = "NewsModule"}
);
Here is my Account Area Routes Registration,
Location /Areas/Account/
public class AccountAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Account"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Account_default",
"Account/{controller}/{action}/{id}",
new { controller = "", action = "", id = "" }
);
}
}
来源:https://stackoverflow.com/questions/1769949/asp-net-mvc-2-beta-single-project-area-registration-getting-http-404