DotNETCore 学习笔记 路由
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 学习笔记 路由的更多相关文章
- Angular6 学习笔记——路由详解
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- ASP.NET MVC4学习笔记路由系统实现
一.路由实现 路由系统实际是一个实现了ASP.NET IHttpModule接口的模块,通过注册HttpApplication的PostResolveRequestCache 事件对Url路由处理.总 ...
- ASP.NET MVC4学习笔记路由系统概念与应用篇
一.概念 1.路由是计算机网络中的一个技术概念,表示把数据包从一个网段转发至另一网段.ASP.NET中的路由系统作用类似,其作用是把请求Url映射到相应的"资源"上,资源可以是一段 ...
- WPF 学习笔记 路由事件
1. 可传递的消息: WPF的UI是由布局组建和控件构成的树形结构,当这棵树上的某个节点激发出某个事件时,程序员可以选择以传统的直接事件模式让响应者来响应之,也可以让这个事件在UI组件树沿着一定的方向 ...
- Nancy in .NET Core学习笔记 - 路由
前文中,我介绍了Nancy的来源和优点,并创建了一个简单的Nancy应用,在网页中输出了一个"Hello World",本篇我来总结一下Nancy中的路由 Nancy中的路由的定义 ...
- angularJs学习笔记-路由
1.angular路由介绍 angular路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样. 后台路由,通过不同的 url 会路由到不同的控制器 (controller) 上,再渲染(re ...
- vue 学习笔记—路由篇
一.关于三种路由 动态路由 就是path:good/:ops 这种 用 $route.params接收 <router-link>是用来跳转 <router-view> ...
- vue学习笔记——路由
1 路由配置 在vue.config中配置,则在代码中可以使用 @来表示src目录下 import aa from '@/aa/index.js' 2 单页面可以懒加载 3 创建动态路由 路由中定义: ...
- angular5学习笔记 路由通信
首先在路由字典中,接收值的组件中加上:/:id 在发送值的组件中,发送值的方式有几种. 第一种:<a routerLink="/detail/1">新闻详情1</ ...
随机推荐
- select into from 和 insert into select
select into from 和 insert into select都是用来复制表, 两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建. inser ...
- filter() 函数的使用
Python3 filter() 函数 描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换. 该接收两个参数,第一个 ...
- 安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010, ...
- 线程间通信(等待,唤醒)&Java中sleep()和wait()比较
1.什么是线程间通信? 多个线程在处理同一资源,但是任务却不同. 生活中栗子:有一堆煤,有2辆车往里面送煤,有2辆车往外拉煤,这个煤就是同一资源,送煤和拉煤就是任务不同. 2.等待/唤醒机制. 涉及的 ...
- CentOS 单用户模式:修改Root密码和grub加密[转]
原文出处: http://zhengdl126.iteye.com/blog/430268 Linux 系统处于正常状态时,服务器主机开机(或重新启动)后,能够由系统引导器程序自动引导 Linux 系 ...
- 新浪微博API Oauth2.0 认证
原文链接: http://rsj217.diandian.com/post/2013-04-17/40050093587 本意是在注销账号前保留之前的一些数据.决定用python 爬取收藏.可是未登录 ...
- USACO Section1.3 Wormholes 解题报告
wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...
- Android FrameWork 概述
Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...
- 深入理解synchronize
本文参考引用,本人整理个人理解.地址点击 1.实现原理 synchronized可以保证方法或者代码块在运行时,同一时刻只有一个方法可以进入到临界区,同时它还可以保证共享变量的内存可见性. 下面是一些 ...
- (原)Unreal渲染相关的缓冲区 及其 自定义代码几种抓取
@authot: 白袍小道 转载说明那啥即可. (图片和本文无关,嘿嘿,坑一下) 以下为Unreal4.18版本中对GPUBuffer部分的分析结果 (插入:比之够着,知至目的) ...