zoukankan      html  css  js  c++  java
  • C# Area 双重路由

    在WebApi项目里面

    一般除了接口, 还有管理端...一些乱七八糟的,你想展示的东西, 一种做法是分开写:

    比如管理后台一个项目, 然后接口一个, 然后页面一个, 其实这样做也可以,但是这么做, 无论是我们部署的时候,

    还是调试的时候,都带来了极大的不便。 项目本身 冗余的地方也有很多, 比如说Model层, 比如说BLL, DAL这些,很多重用的方法、

    逻辑处理,这些都是不必要的东西。 接下来, 给大家推荐一种 Area 的方式,来解决这个问题。

    添加区域, 出现了对应的

    在Areas 里面, 会有独立的一套 MVC 

    我们先来看看, 这里面的路由是如何写的

    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 {action = "Index", id = UrlParameter.Optional }
                );
            }
        }

    继承于  Area 路由,其他的地方, 和我们正常的路由注册相同。

    那这段区域路由,如何添加到路由表里, 供我们访问呢?

    Global.asax 

    注册 Areas 路由的地方  有一段 AreaRegistration.RegisterAllAreas(); 

    是注册所有的继承于 AreaRegistration的路由~

    WebApi 的路由还没有注册呢~ 

    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 = "Index", action = "Index", id = UrlParameter.Optional },
    namespaces: new string[] { "WebApi.Areas.Luma.Controllers" }
    ).DataTokens.Add("Area", "Luma");
    
    //Api路由
    routes.MapRoute(
    name: "apiroute",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Help", action = "Index", id = UrlParameter.Optional }
    );
    }
    }

    其中有一段路由比较特别, 插入了一段 namespaces 的注册信息, 这一段内容, 就是双路由中, 默认页的关键

    源文:https://www.cnblogs.com/29boke/p/5824603.html  C# Area 双重路由如何写

     

  • 相关阅读:
    79. Word Search
    97. Interleaving String
    74. Search a 2D Matrix
    73. Set Matrix Zeroes
    72. Edit Distance
    71. Simplify Path
    64. Minimum Path Sum
    shell编程 备份mysql数据库并发送到另一个服务器
    linux 命令执行的判断依据: ;,&&,||
    linux 数据流重定向
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/11337612.html
Copyright © 2011-2022 走看看