2

I have an MVC5 website with a few areas in it. Each area has its own set of controllers and obviously every controller has a default Index.cshtml as landing page. So far so good.

But how do I go about implementing a landing page for an Area? I don't think there can be a landing page for an area independent of the controllers, so perhaps I would need to use sort of an area Home controller that would volunteer a landing page.

The thing is that I want an URL like this to work:

http://www.domain.exe/AreaN/

currently that does not work unless I make it like this:

http//www.domain.exe/AreaN/Controller/

at this point my Area registration route looks like this

context.MapRoute(
            "AreaN_default",
            "AreaN/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new string[] { "Namespace" }
        );
Lord of Scripts
  • 3,579
  • 5
  • 41
  • 62

1 Answers1

1

I'm not too familiar with Areas, but couldn't you just define controller in your route options?

context.MapRoute(
            "AreaN_default",
            "AreaN/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Namespace" }
        );

Then AreaN/Home/Index would be your default view.

As NightOwl888 explains in his comment, this makes the controller optional, because the default controller is now the home controller.

bradlis7
  • 3,375
  • 3
  • 26
  • 34
  • 1
    This answer is correct, but could use a bit more explanation. The reason why the Area URL the OP posted doesn't work is because `controller` has no default value, which makes it a *required* value. The reason why this one works is because you have made the `controller` placeholder optional by specifying a value as a default value. It is unfortunate that the tooling of Visual Studio doesn't set this as the default value, which makes it act differently than the default route (which sets all parameters optional). I think they did this to avoid collisions when controllers/areas are the same name. – NightOwl888 Feb 03 '16 at 19:05