InvalidRouteException on MVC3 application with multiple areas

73 views Asked by At

I have a MVC3 web application with one default area and one admin area. Both have their won home pages after logging in. After successfully logging in to the application if i try to access the admin area i am getting InvalidRouteException.

Scenario is like that i login to the application successfully. When i try to access the admin area using

http://localhost/admin

i get following exception.

Server Error in '/' Application.

{controller}/{action}/{id} { controller: 'admin'; action: 'LogOn' } Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: LoginSystem.Web.Exceptions.InvalidRouteException: {controller}/{action}/{id} { controller: 'admin'; action: 'LogOn' }

However if i log-out and then use URL like

http://localhost/admin

it takes me to the login page successfully.

Please suggest.

Below is my RegisterArea function for the consumer area.

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Consumer_default",
                "Consumer/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }, new[] { "MyPortal.Web.Areas.Consumer.Controllers" }
            );

            context.MapRoute(
            "Admin", // Route name
            "Admin/{action}/{id}", // URL with parameters
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, new[] { "MyPortal.Web.Controllers" }// Parameter defaults
        );
       }

More Information - Update 1 After registering "Admin" Route i am able to move to the Logon page. My application has 2 areas Client and ADmin(this is default one.) Both areas are authenticated. Now the scenario is that i logged in to client module. While i am logged in, i did

http://localhost/admin

and reached the Logon page. But the issue is that now the url donot contain the Return URL. What should i do so that it contains returnurl.

In normal scenario when we are not logged in the Logon page opens with return url added to its url.

Please suggest.

1

There are 1 answers

4
Ryan Butler On

Reads like you haven't configured global.asax.cs & AccountController.cs correctly. I had a similar problem the other day and took me a little to remember I'm doing customizations of my routing. I have an admin section with a localhost address much like yours. Here's what I did:

In your global.asax.cs file, add the following:

 routes.MapRoute(
            "Admin", // Route name
            "Admin/{action}/{id}", // URL with parameters
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

The above describes a route with a name of admin. The second line, says to look for a url that looks like:

http://localhost/admin/

With an appropriate "home" action.

Your admin controller should start with:

 //
    // GET: /Admin/
    [Authorize]
    public ViewResult Index()
    {
        return View(db.Tutorial.ToList());
    }

Then when dealing with you authorization, you'll want to adjust the Account controller on the [Http] post of LogOn, specifically the RedirectToAction to:

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Admin");
                }

The other area that needs adjustment is [Http] post:

 [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index","Admin");
            }

Course, I'm assuming you're talking about the default security/membership provider.

HTH.