How to register areas for routing

I created MVC Application that have 3 different Area. (Admin, User, News)
This is my RouteConfig.cs File in App_Start directory:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "TestMvcApplication.Controllers" }
        );
    }
}

And This is my AdminAreaRegisteration.cs file:
    namespace TestMvcApplication.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "TestMvcApplication.Areas.Admin.Controllers" }                
            );
        }
    }
}

And finally this is my Global.asax.cs file content:
namespace TestMvcApplication
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }
}

Home page of my website fully loaded and it’s works. but Homepage of Admin or other areas are not detect by route and I gave this error message:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Admin/Home

How can I solve this problem?
Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Call AreaRegistration.RegisterAllAreas() somewhere in your RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    ....
}

Tip: Use a tool like RouteDebugger 2.0 or Routing Debugger to investigate your routes

Get latest NuGet:
Route Debugger for MVC or RouteDebugger for WepApi

Here’s a tutorial on How to set up and use RouteDebugger with WebApi

Method 2

From the code provided I can see 2 potential issues:

  1. You aren’t calling RegisterAllAreas
  2. You don’t appear to be overriding the AreaName property

Try changing your code to:

Global.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "TestMvcApplication.Controllers" }
    );
}

Admin area
public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Method 3

Just create a static class name it AreaConfig with a static method RegisterAreas() here code:

public static class AreaConfig
{
    public static void RegisterAreas()
    {
        // 
        // Admin area . . .

        var adminArea = new AdminAreaRegistration();
        var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes);
        adminArea.RegisterArea(adminAreaContext);


        // 
        // Default area . . .

        var defaultArea = new DefaultAreaRegistration();
        var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes);
        defaultArea.RegisterArea(defaultAreaContext);
    }
}

then call it in a Global.asax.cs file like this:
protected void Application_Start()
    {
        . . .

        AreaConfig.RegisterAreas();

        . . .
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x