开发者

ASP.NET MVC3 Using Areas

I'm having some troubles setting up two Areas in my project, I created both Areas by right clicking my project -> Add -> Area using VS 2010 Professional,

The code it generated in the *AreaRegistration.cs files is exactly as I'd expect it.

Both my areas have multiple controllers.

I've checked my global.asax.cs file and it contains what I'd expect, the following is at the bottom:

// Generated code
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

When I change the default genera开发者_StackOverflowted routing info I can get it to work for a Single controller:

context.MapRoute(
    "CommunityCourses_default",
    "CommunityCourses/{controller}/{action}/{id}",
    new {controller="NameOfController", action = "Index", id = UrlParameter.Optional }
);

The controller="NameOfController" is what I've tried adding to the generated code, but it only works for a single controller.

Edit: I renamed the namespace of my files to use PROJECTNAME.AREANAME within an area, is this causing issues? I thought it'd simplify my imports in a non-convention-breaking way.

What am I missing? I've noticed a lot of problems about areas on SO and tried to follow the answers without success. This issue has bothered eluded me for the majority of the day, any help would be greatly appreciated!


If you moved files from the Controllers folder or the VIews folder in the root of the project into Controllers or View folders contained in the {AreaName} folder, then all of those files moved need their namespaces changed from {ProjectName}.{*etCetera} to:

{ProjectName}.Areas.{AreaName}.{*etCetera}


ASP.NET MVC3 Using Areas

Turns out what was wrong was that the order or routing was incorrect, it was processing {controller}/{action}/{id} first which was breaking my areas. Moving the

AreaRegistration.RegisterAllAreas();

to the top of my application_start fixed my problem.

http://haacked.com/archive/2011/04/13/routedebugger-2.aspx Link to the tool I used to find the problem, unfortunately the current version of the tool doesn't work on the default 404 pages, so I also had to create a custom 404 page for it to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜