2

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 generated 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!

2 Answers 2

2

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}
Sign up to request clarification or add additional context in comments.

4 Comments

I changed the namespace to {ProjectName}.{AreaName} of all files in each area, did this break the way the route finder works? I'm starting to think thats where the problem is coming from, can I use {ProjectName}.Areas.{AreaName} as a namespace for all files within my Area?
Not quite. For example, controllers should be in the namespace {ProjectName}.Areas.{AreaName}.Controllers.
My question is does MVC3 require this namespace naming convention? I know it's the default but is it necessary?
I have never tried it otherwise. There would be several consequences of not using the convention, and your solution would have to address all of those issues regarding finding components.
1

Route Debugger showing the routing is incorrect

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.