I stumbled upon a problem when trying to set up a default route for an asp.net mvc3 application. The routing mechanism itself is pretty straightforward, so I didn’t really expect any problems here, but as it turned out I was wrong.
Here’s the stripped down version of the problem:
Create a default mvc application – you’ll get the HomeController along with two actions there. Now add a new area (right click on the project and select “Add Area”) – let’s call it “Foo”. In that area, create a controller named “HomeController” and an Index action along with a view for it.
So now we have two Home controllers both with an Index action that simply renders a view.
The route set up by default (in global.asax.cs) looks like this:
The route set up for the area (FooAreaRegistration.cs) lookans like this:
I made a small modification to it – added a default value for the controller part (now that I think of it, the controller default value was probably left out on purpose).
Run the app and the problem arises:
Now this is somewhat weird at first – the url does not have an area prefix, so it should be able to determine which controller to run, but that’s not the case.
I’m not entirely sure why this happens. I’ve tried experimenting with various url schemes including the area itself in the url scheme, but with no luck.
When areas were introduced, they did add the “area” key in RouteData dictionary. It’s value is populated automatically, but I was not able to make it play as I wanted.
The easiest way to get rid of the error is to use different controller names. The second option is to do this:
Both of these suggestions came from here.
But that’s not the end. What I actually wanted to achieve was to set the area controller as the default one. So when someone typed in www.mydomain.com the Areas.Foo.Controllers.HomeController would be used instead of the Controllers.HomeController one. And with the second method this actually happened, except that the view being rendered was not the one from Foo area but the one from the ‘root’ area – this would probably be classified as a bug.
So after struggling with this back and forth I decided to go the KISS way – renamed the root controller from Home to FrontPage and set up the action to redirect to the view I wanted. Maybe not the solution I hoped for, but it works.