概述


本文已经同步到《Asp.net Vnext 系列教程 》中]

ASP.NET 路由系统是主要负责两个操作:

它将传入的 HTTP 请求映射到路由处理程序给出的路由的集合。

路由系统的责任是找到匹配的路由,创建路由数据,并将请求分配给一个处理程序。
选择动作是 MVC 的处理程序的实现细节。它使用路由数据和从传入请求其他信息来选择要执行的操作

代码实现TemplateRoute 类初始化路由和 URL 模板


  public class MyTemplateRoute : TemplateRoute
{
public MyTemplateRoute(IRouteBuilder routeCollectionBuilder)
: base(routeCollectionBuilder.DefaultHandler,
"{controller}/{action}/{id?}",
new RouteValueDictionary(new { controller = "Home", action = "Index" }),
new RouteValueDictionary(new { }),
new RouteValueDictionary(new { }),
routeCollectionBuilder.ServiceProvider.GetService<IInlineConstraintResolver>())
{
} public override Task RouteAsync(RouteContext context)
{
return base.RouteAsync(context);
}
}

启动类

  public class Startup
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes => {
//加入模板 默认http://http://localhost/Home/Index
routes.Routes.Add(new MyTemplateRoute(routes)); });
}
}

实现IRouter添加默认路由

 public class DefaultRoute : IRouter
{ private readonly IRouter _next; public DefaultRoute(IRouter next)
{
_next = next;
} public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return _next.GetVirtualPath(context);
} public async Task RouteAsync(RouteContext context)
{ var oldRouteData = context.RouteData;
var newRouteData = new RouteData(oldRouteData);
newRouteData.Routers.Add(_next);
newRouteData.Values["controller"] = "Home";
newRouteData.Values["action"] = "Index";
try
{
context.RouteData = newRouteData;
await _next.RouteAsync(context);
}
finally
{
if (!context.IsHandled)
{
context.RouteData = oldRouteData;
} }
} }
  public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes => {
//加入模板 默认http://http://localhost/Home/Index
// routes.Routes.Add(new MyTemplateRoute(routes)); routes.MapRoute("default", "{controller}/{action}");
//加入路由处理 默认http://http://localhost/Home/Index
routes.Routes.Add(new DefaultRoute(routes.DefaultHandler)); });
}

实现IRouteConstraint约束

  public class DateConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary<string, object> values, RouteDirection routeDirection)
{
return values["controller"] == "Home";
}
}
  public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes => {
//加入模板 默认http://http://localhost/Home/Index
// routes.Routes.Add(new MyTemplateRoute(routes)); //routes.MapRoute("default", "{controller}/{action}");
////加入路由处理 默认http://http://localhost/Home/Index
//routes.Routes.Add(new DefaultRoute(routes.DefaultHandler)); //加入约束
routes.MapRoute(name: "TestRoute", template: "{*constraints}", defaults: new { controller = "Home", action = "Index" }, constraints: new { constraint = new DateConstraint() }); });
}

路由特性

public class HomeController : Controller
{ //PUT http://localhost/AB
[AcceptVerbs("PUT", Route = "AB")]
// Patch http://localhost/AB
[HttpPatch("AB")]
//PUT http://localhost/Home/AB
[AcceptVerbs("PUT", Route = "Home/AB")]
//Patch http://localhost/Home/AB
[HttpPatch("Home/Ab")] // ABC 动作 可以被以下地址访问
//PUT http://localhost/AB
// Patch http://localhost/AB
//PUT http://localhost/Home/AB
//Patch http://localhost/Home/AB
public IActionResult ABC()
{ return Content("");
}
} }

RouteConstraintAttribute 路由约束

   public class CountrySpecificAttribute : RouteConstraintAttribute
{
public CountrySpecificAttribute(string countryCode)
: base("country", countryCode, blockNonAttributedActions: true)
{
}
}

应用在控制上

添加路由条目

  routes.MapRoute(
"products",
"Products/{country}/{action}",
defaults: new { controller = "Products" })yu

运行截图

Area

//区域名 
[Area("Admin")]
//路由
[Route("[area]/Users")]
public class UserManagementController : Controller
{ [HttpGet("All")]
public IActionResult ListUsers()
{
return Content("");
}
}

添加路由条目

    routes.MapRoute("areaRoute",
"{area:exists}/{controller}/{action}",
new { controller = "Home", action = "Index" });

Asp.net Vnext Routing的更多相关文章

  1. POCO Controller 你这么厉害,ASP.NET vNext 知道吗?

    写在前面 阅读目录: POCO 是什么? 为什么会有 POJO? POJO 的意义 POJO 与 PO.VO 的区别 POJO 的扩展 POCO VS DTO Controller 是什么? 关于 P ...

  2. 分享我对 ASP.NET vNext 的一些感受,也许多年回过头看 So Easy!

    写在前面 阅读目录: Visual Studio "14" CTP 关于 ASP.NET vNext ASP.NET vNext 实践 后记 ASP.NET vNext 发布已经过 ...

  3. 开发 ASP.NET vNext 初步总结(使用Visual Studio 14 CTP1)

    新特性: vNext又称MVC 6.0,不再需要依赖System.Web,占用的内存大大减少(从前无论是多么简单的一个请求,System.Web本身就要占用31KB内存). 可以self-host模式 ...

  4. [译]Introducing ASP.NET vNext and MVC 6

    原文:http://www.infoq.com/news/2014/05/ASP.NET-vNext?utm_source=tuicool Part of the ASP.NET vNext init ...

  5. Asp.net vNext 学习1

    Asp.net vNext 学习之路(一) 概述 asp.net vNext 也叫 asp.net 5.0,意思是微软推出的下一个版本的asp.net.可以说是微软对asp.net的一个比较重大的重新 ...

  6. Introducing ASP.NET vNext and MVC 6

    [译]Introducing ASP.NET vNext and MVC 6 原文:http://www.infoq.com/news/2014/05/ASP.NET-vNext?utm_source ...

  7. [转载]Getting Started with ASP.NET vNext and Visual Studio 14

    说在转载之前的话:ASP.NET框架之前不断做大,而vNext则是从头开始,对ASP.NET框架进行拆分并瘦身,面对不同的需求而更加灵活,各个拆分出来的模块更加轻量.vNext的出现,对ASP.NET ...

  8. Asp.net vNext 学习之路(一)

    概述 asp.net vNext 也叫 asp.net 5.0,意思是微软推出的下一个版本的asp.net.可以说是微软对asp.net的一个比较重大的重新设计, asp.net vNext是一 个比 ...

  9. 兼容Mono的下一代云环境Web开发框架ASP.NET vNext

    微软在2014年5月12日的TechEd大会上宣布将会发布下一代ASP.NET框架ASP.NET vNext的预览.此次发布的ASP.NET框架与以前相比发生了根本性的变化,凸显了微软“云优先”(cl ...

随机推荐

  1. 重写NSLog,Debug模式下打印日志和当前行数

    在pch文件中加入以下命令,NSLog在真机测试中就不会打印了 //重写NSLog,Debug模式下打印日志和当前行数 #if DEBUG #define NSLog(FORMAT, ...) fpr ...

  2. cacti批量添加主机脚本

    #!/bin/bash ##cacti批量脚本位置 device=/var/www/html/cacti/cli/add_device.php graphs=/var/www/html/cacti/c ...

  3. mac osx 启动wireshark闪退

    wireshark启动会提示安装x11 去x11地址安装后 启动还是闪退 原来是姿势不对 这样才行~~ 这一步 这个路径一定要对!路径一定要对!路径一定要对! 然后报错不用管它,如果没反应了,就继续等 ...

  4. 主机OS重装的节点加回RAC集群步骤示例(11gR2 RAC)

    原文地址: https://blogs.oracle.com/Database4CN/entry/%E4%B8%BB%E6%9C%BAos%E9%87%8D%E8%A3%85%E7%9A%84%E8% ...

  5. jquery 操作select

    jQuery("#select_id").change(function(){}); // 1.为Select添加事件,当选择其中一项时触发 var checkValue = jQ ...

  6. iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)

    UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *vi ...

  7. PostgreSQL9.1 upgrade to PostgreSQL9.5rc1

    PostgreSQL9.1.0 upgrade to PostgreSQL9.5rc1 安装PG9.1端口为5432 [pgup@minion1 pg]$ ls postgresql-9.1.0.ta ...

  8. PostgreSQL Replication之第十一章 使用Skytools(3)

    11.3 管理 pgq-queues Skytools 的一个核心组件是pgq.它提供了一个通用排队接口,它可以让您把消息从一个消息提供者传送到一个任意数目的接收者. 现在的问题是:一般来说,一个队列 ...

  9. 最长上升子序列(N*log(N))hdu1025

    (HDU1025) Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  10. ruby初步学习中遇到的错误

    print <<off This is the second way of creating here document ie. multiple line string; off 报错: ...