C# Area 双重路由

在WebApi项目里面html

通常除了接口, 还有管理端...一些乱七八糟的,你想展现的东西, 一种作法是分开写:api

好比管理后台一个项目, 而后接口一个, 而后页面一个, 其实这样作也能够,可是这么作, 不管是咱们部署的时候,ide

仍是调试的时候,都带来了极大的不便。 项目自己 冗余的地方也有不少, 好比说Model层, 好比说BLL, DAL这些,不少重用的方法、post

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

 

 

添加区域, 出现了对应的spa

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

 

咱们先来看看, 这里面的路由是如何写的调试

 

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 路由,其余的地方, 和咱们正常的路由注册相同。code

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

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

 

源文:http://www.javashuo.com/article/p-ocfhtlat-bk.html  C# Area 双重路由如何写

相关文章
相关标签/搜索