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 的注册信息, 这一段内容, 就是双路由中, 默认页的关键

    这些希望对大家能有所帮助吧~ 

    改天再分享 webapi help 的部署~ 

  • 相关阅读:
    "alert(1) to win" writeup
    "CoolShell puzzle game" writeup
    Maximum Subarray(最大连续子序列和)
    hacking 学习站
    爬虫:备份人人网状态
    ichunqiu在线挑战--网站综合渗透实验 writeup
    ichunqiu在线挑战--我很简单,请不要欺负我 writeup
    IDF-CTF-简单的js加密 writeup
    IDF-CTF-cookie欺骗 writeup
    IDF-CTF-不难不易的js加密 writeup
  • 原文地址:https://www.cnblogs.com/29boke/p/5824603.html
Copyright © 2011-2022 走看看