14

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Oundless
  • 5,425
  • 4
  • 31
  • 33
  • 1
    On further investigation, it appears that the request is being directed to the correct controller (i.e. MyApp.Areas.Common.Controllers.HomeController) with or without your suggested change. However, in both cases, the ViewEngine is only searching the ~/Views/Home and ~/Views/Shared folders rather than starting with ~/Areas/Common/Views/Home and ~/Areas/Common/Views/Shared. Strangely, if I create a page with an ActionLink to the same controller method (i.e. Index()) then it works OK. Hmmm. – Oundless Jan 06 '10 at 10:41
  • http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc That may help. I had a similar issue. – LiamB Feb 11 '10 at 08:31

4 Answers4

14

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.

Makes sure you're also letting MVC know to register the areas you have in the application:

Put the following in your Application_Start method in the Global.asax.cs file:

AreaRegistration.RegisterAllAreas();
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
George Stocker
  • 57,289
  • 29
  • 176
  • 237
7

What you have to do is:

  • Remove Default Route from global.asax.cs

    //// default route map will be create under area
    //routes.MapRoute(
    //    name: "Default",
    //    url: "{controller}/{action}/{id}",
    //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
    
  • Update SecurityAreaRegistration.cs in area Common

  • Add following route mapping:

     context.MapRoute(
        "Default",
        "",
        new { controller = "Home", action = "Index", id = "" }
    );
    
CharlesB
  • 86,532
  • 28
  • 194
  • 218
user279993
  • 135
  • 6
0

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
  • 24,380
  • 11
  • 62
  • 94
0

In the Global.asax delete the .MapRoute

and make it look like

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
}

then under the area's yourareaAreaRegistration.cs (its there if you added the area through VS)

public override void RegisterArea(AreaRegistrationContext context)
{
     //This is the key one         
     context.MapRoute("Root", "", new { controller = "Home", action = "Login" });

     //Add more MapRoutes if needed
}
davidsleeps
  • 9,393
  • 11
  • 59
  • 73
Egli Becerra
  • 961
  • 1
  • 13
  • 25