views:

451

answers:

3

I'm trying to set the Default URL of my MVC application to a view within an area of my application. The area is called "Common", the controller "Home" and the view "Index".

I've tried setting the defaultUrl in the forms section of web.config to "~/Common/Home/Index" with no success.

I've also tried mapping a new route in global.asax, thus:

routes.MapRoute(
        "Area",
        "{area}/{controller}/{action}/{id}",
        new { area = "Common", controller = "Home", action = "Index", id = "" }
    );

Again, to no avail.

+3  A: 

The route you listed only works if they explicitly type out the URL:

yoursite.com/{area}/{controller}/{action}/{id}

What that route says is:

If I get a request that has a valid {area}, a valid {controller} in that area, and a valid {action} in that controller, then route it there.

What you want is to default to that controller if they just visit your site, yoursite.com:

routes.MapRoute(
    "Area",
    "",
    new { area = "Common", controller = "Home", action = "Index" }
);

What this says is that if they don't append anything to http://yoursite.com then to route it to the following action: Common/Home/Index

Also, put it at the top of your routes table.

George Stocker
A: 

What you're doing seems correct. If I had to guess I would say this is happening due to the way you are running your website. In Visual Studio, if you have a specific view selected when you hit F5 that view will be the starting Url - try selecting the Project and then hitting F5?

Jaco Pretorius
I'm afraid that's not the cause of the problem. Thanks for your suggestion though.
Moose Factory
It was worth a try :D
Jaco Pretorius
A: 

What you have to do is: - Update SecurityAreaRegistration.cs in area Common - Add following route mapping:

context.MapRoute( "Default", "", new { controller = "Home", action = "Index", id = "" } );