Search code examples
c#.netasp.net-mvc-3http-redirectasp.net-mvc-3-areas

MVC 3 Area Registration 302 Error


I have a 2 area which names called with "Admin" and "Services". My project structure as below image 1

I want to redirect all root url to Admin area. I changed my area registration files to resolve this problem as below code block.

//Root 
//Old Value
routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
//New Value
routes.MapRoute(
                "Default", // Route name
                "trash/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

Now for redirecting the root url to Admin area I've updated AdminAreaRegistration.cs class as below.

//Admin
//Old Value
context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

//New Value
context.MapRoute(
                "Admin_default",
                "{controller}/{action}/{id}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

After this update when i want to browse my web site it was ok I can see my admin area views. But I have a lot of AJAX call on Admin Area Views and when those ajax calls try to call Services Area controller it could not access to the Services Controller, I've monitored on network plugin to see what is my error I get a HTTP 302 error and then its redirect to 404 Not Found page.

I hope my explanation is understable.

Thank you in advance.

Project Structure


Solution

  • If you add below parameters to your MapRoute it will be fixed.

    .DataTokens.Add("area", "Admin"); 
    

    And

     namespaces: new[] { "X.Areas.Admin.Controllers" } ).
    

    Final Output

    routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new {area="Admin",  controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { "X.Areas.Admin.Controllers" }
                    ).DataTokens.Add("area", "Admin");