Route
------------------------------------------constraint----------------------------------------------------
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id:int}"); routes.MapRoute(
name: "default_route",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }); routes.MapRoute(
name: "blog",
template: "Blog/{*article}",
defaults: new { controller = "Blog", action = "ReadArticle" }); routes.MapRoute(
name: "us_english_products",
template: "en-US/Products/{id}",
defaults: new { controller = "Products", action = "Details" },
constraints: new { id = new IntRouteConstraint() },
dataTokens: new { locale = "en-US" }); Using Routing Middleware To use routing middleware, add it to the dependencies in project.json:
"Microsoft.AspNetCore.Routing": <current version> public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
var trackPackageRouteHandler = new RouteHandler(context =>
{
var routeValues = context.GetRouteData().Values;
return context.Response.WriteAsync(
$"Hello! Route values: {string.Join(", ", routeValues)}");
}); var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler); routeBuilder.MapRoute(
"Track Package Route",
"package/{operation:regex(^track|create|detonate$)}/{id:int}"); routeBuilder.MapGet("hello/{name}", context =>
{
var name = context.GetRouteValue("name");
// This is the route handler when HTTP GET "hello/<anything>" matches
// To match HTTP GET "hello/<anything>/<anything>,
// use routeBuilder.MapGet("hello/{*name}"
return context.Response.WriteAsync($"Hi, {name}!");
}); var routes = routeBuilder.Build();
app.UseRouter(routes); URI Response
/package/create/ Hello! Route values: [operation, create], [id, ]
/package/track/- Hello! Route values: [operation, track], [id, -]
/package/track/-/ Hello! Route values: [operation, track], [id, -]
/package/track/ <Fall through, no match>
GET /hello/Joe Hi, Joe!
POST /hello/Joe <Fall through, matches HTTP GET only>
GET /hello/Joe/Smith <Fall through, no match> The framework provides a set of extension methods for creating routes such as:
•MapRoute
•MapGet
•MapPost
•MapPut
•MapDelete
•MapVerb Route Constraint constraint Example Example Match Notes
int {id:int} Matches any integer
bool {active:bool} true Matches true or false
datetime {dob:datetime} -- Matches a valid DateTime value
decimal {price:decimal} 49.99 Matches a valid decimal value
double {weight:double} 4.234 Matches a valid double value
float {weight:float} 3.14 Matches a valid float value
guid {id:guid} 7342570B-<snip> Matches a valid Guid value
long {ticks:long} Matches a valid long value
minlength(value) {username:minlength()} steve String must be at least characters long.
maxlength(value) {filename:maxlength()} somefile String must be no more than characters long.
length(min,max) {filename:length(,)} Somefile.txt String must be at least and no more than
min(value) {age:min()} Value must be at least .
max(value) {age:max()} Value must be no more than .
range(min,max) {age:range(,)} Value must be at least but no more than .
alpha {name:alpha} Steve String must consist of alphabetical characters.
regex(expression){ssn:regex(^d{}-d{}-d{}$)} -- ***
required {name:required} Steve *** URL Generation :
app.Run(async (context) =>
{
var dictionary = new RouteValueDictionary
{
{ "operation", "create" },
{ "id", }
}; var vpc = new VirtualPathContext(context, null, dictionary, "Track Package Route");
var path = routes.GetVirtualPath(vpc).VirtualPath; context.Response.ContentType = "text/html";
await context.Response.WriteAsync("Menu<hr/>");
await context.Response.WriteAsync($"<a href='{path}'>Create Package 123</a><br/>");
}); routes.MapRoute("blog_route", "blog/{*slug}",
defaults: new { controller = "Blog", action = "ReadPost" });

DotNETCore 学习笔记 路由的更多相关文章

  1. Angular6 学习笔记——路由详解

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  2. ASP.NET MVC4学习笔记路由系统实现

    一.路由实现 路由系统实际是一个实现了ASP.NET IHttpModule接口的模块,通过注册HttpApplication的PostResolveRequestCache 事件对Url路由处理.总 ...

  3. ASP.NET MVC4学习笔记路由系统概念与应用篇

    一.概念 1.路由是计算机网络中的一个技术概念,表示把数据包从一个网段转发至另一网段.ASP.NET中的路由系统作用类似,其作用是把请求Url映射到相应的"资源"上,资源可以是一段 ...

  4. WPF 学习笔记 路由事件

    1. 可传递的消息: WPF的UI是由布局组建和控件构成的树形结构,当这棵树上的某个节点激发出某个事件时,程序员可以选择以传统的直接事件模式让响应者来响应之,也可以让这个事件在UI组件树沿着一定的方向 ...

  5. Nancy in .NET Core学习笔记 - 路由

    前文中,我介绍了Nancy的来源和优点,并创建了一个简单的Nancy应用,在网页中输出了一个"Hello World",本篇我来总结一下Nancy中的路由 Nancy中的路由的定义 ...

  6. angularJs学习笔记-路由

    1.angular路由介绍 angular路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样. 后台路由,通过不同的 url 会路由到不同的控制器 (controller) 上,再渲染(re ...

  7. vue 学习笔记—路由篇

    一.关于三种路由 动态路由 就是path:good/:ops    这种 用 $route.params接收 <router-link>是用来跳转  <router-view> ...

  8. vue学习笔记——路由

    1 路由配置 在vue.config中配置,则在代码中可以使用 @来表示src目录下 import aa from '@/aa/index.js' 2 单页面可以懒加载 3 创建动态路由 路由中定义: ...

  9. angular5学习笔记 路由通信

    首先在路由字典中,接收值的组件中加上:/:id 在发送值的组件中,发送值的方式有几种. 第一种:<a routerLink="/detail/1">新闻详情1</ ...

随机推荐

  1. Still unable to dial persistent://blog.csdn.net:80 after 3 attempts

    动不动电脑有些网站打不开了,还报错: Still unable to dial persistent://blog.csdn.net:80 after 3 attempts 为什么呢? 是dns坏了? ...

  2. Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏

    其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...

  3. 解决NSTimer循环引用

    NSTimer常见用法 @interface XXClass : NSObject - (void)start; - (void)stop; @end @implementation XXClass ...

  4. CodeIgniter学习笔记二:CI中的query_builder(AR)、连贯操作

    一.开启query_builder 在application\config\database.php中添加如下代码(默认已开启): $query_builder = TRUE; 二.查询数据 //ge ...

  5. JAVA中的类

    节选自:http://www.cnblogs.com/dolphin0520/p/3811445.html 1. 成员内部类是依附外部类而存在的,也就是说,如果要创建成员内部类的对象,前提是必须存在一 ...

  6. SpringMVC 整合 kaptcha(验证码功能)

    一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...

  7. JMeter获取复杂的JSON串中的参数的值

    大家好,这篇博文中主要是介绍怎么用JMeter的BeanShell去获取复杂的JSON串中的某个参数的值,这将 便于我们用JMeter做出更完美的自动化测试: 首先有这样一个json串: { &quo ...

  8. django文件上传、图片验证码、抽屉数据库设计

    1.Django文件上传之Form方式 settings.py, ALLOWED_HOSTS = ['*'] INSTALLED_APPS = [ 'django.contrib.admin', 'd ...

  9. day06_01 上节回顾

    1.0 extend 扩展方法及"+"的对比 "+"不会改变数组的内容,而extend会改变数组的内容 2.0 sort扩展sorted() a = [1,2, ...

  10. 把SVN版本控制讲给 非IT同事 听

    场景: 什么是版本: 什么是版本控制: 为什么要用版本控制: 推荐使用SVN: 如何快速理解SVN: SVN简单使用: