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. 从coding.net 克隆(git clone)项目代码到本地报无权限(403)错误 解决方案

    直接从coding.net (git clone)项目代码到本地时,会提示没有权限的错误,如下图: 解决方案:添加远程地址的时候带上用户名及密码即可解决,格式如下: git clone http:// ...

  2. 霍夫直线检测 opencv

    本次实验是检测图像中的直线,用到了HoughLines()和HoughLinesP()函数,其中HoughLinesP()称为累计概率霍夫变换,实验结果显示累计概率霍夫变换要比标准霍夫变换的效果好.具 ...

  3. NC-瑞士军刀NetCat

    NC——Telnet/Banner 连接之后可以命令互动,比如POP3\SMTP\HTTP等协议命令 root@kali:/# nc -v pop3..com //-v详细显示 DNS fwd/rev ...

  4. 把实体bean对象转换成DBObject工具类

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util ...

  5. 《Cracking the Coding Interview》——第8章:面向对象设计——题目1

    2014-04-23 17:32 题目:请设计一个数据结构来模拟一副牌,你要如何用这副牌玩21点呢? 解法:说实话,扑克牌的花样在于各种花色.顺子.连对.三带一.炸弹等等,如果能设计一个数据结构,让判 ...

  6. USACO Section1.2 Transformations 解题报告

    transform解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  7. leetcode_day02

    任务二:删除排序数组中的重复项 原文链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 最开始的解决思路: ...

  8. Python——初识Python

    本篇主要内容: • Python的特点 • Python的种类 • Python的编码 • Python的安装环境推荐 • Python的基础用法:输入输出,算术运算符,逻辑运算符,基本程序结构语法 ...

  9. Android记事本开发03

    昨天: 生成签名文件及导出apk 遇到的问题: 无. 今天: activity和intent基础

  10. htmlagilitypack解析html

    这是个很好的的东西,以前做Html解析都是在用htmlparser,用的虽然顺手,但解析速度较慢,碰巧今天找到了这个,就拿过来试,一切出乎意料,非常爽,推荐给各位使用. 下面是一些简单的使用技巧,希望 ...