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.
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:
The above describes a route with a name of admin. The second line, says to look for a url that looks like:
With an appropriate "home" action.
Your admin controller should start with:
Then when dealing with you authorization, you'll want to adjust the Account controller on the [Http] post of LogOn, specifically the RedirectToAction to:
The other area that needs adjustment is [Http] post:
Course, I'm assuming you're talking about the default security/membership provider.
HTH.