3

I've created a new ASP.NET MVC project with areas and I'm trying to set a controller action to be the default controller action if the user visits that area.

I added an area called 'Login' now I have Areas/Login/ and I added LoginController.

I am trying to set this controller to be invoked when the user navigates to the website. I can access it if I type in browser www.test.com/Login/Login but I don't know how to set routing in global.asax to point to this controller as the default.

How do I do that in ASP.NET MVC?

George Stocker
  • 55,025
  • 29
  • 167
  • 231
1110
  • 7,801
  • 45
  • 156
  • 303

2 Answers2

5

When you created your area, did MVC not create the [AreaName]AreaRegistration class under the Areas/[AreaName] folder? In there you will find the area registration that looks similar to this. Modify the controller = portion of the defaults parameter to the controller name (Login) you want to use by default:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Login_default",
            "Login/{controller}/{action}/{id}",
            new { controller = "Login", action = "Index", id = UrlParameter.Optional }
        );
    }
Keith
  • 5,100
  • 3
  • 31
  • 48
2

If you decorate your HomeController (or really, all of your controllers that require someone to be logged in) with the [Authorize] attribute, ASP.NET MVC will automatically redirect people to the login screen if they are not logged in.

Example usage:

[Authorize]
public ActionResult Home()
{
}

Also, you may want to read up on the Open Redirect attack vulnerability (and fix).

George Stocker
  • 55,025
  • 29
  • 167
  • 231
  • I put login here as an example. My main question is how to set routing to start some controller as a default if that controller is in area. – 1110 Jun 24 '11 at 19:51