How to register areas for routing

前端 未结 3 1168
野性不改
野性不改 2020-12-01 07:58

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
{
          


        
相关标签:
3条回答
  • 2020-12-01 08:10

    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 }
            );
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:11

    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();
    
            . . .
        }
    
    0 讨论(0)
  • 2020-12-01 08:12

    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

    0 讨论(0)
提交回复
热议问题