ASP.NET CORE WebAPI 中 Route 属性配置
1 访问路径属性 Route
public class OrdersController : ApiController
{[Route("customers/{customerId}/orders")][HttpGet]public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }
}
字符串
"customers/{customerId}/orders"是路由模板。 Web API根据这个模板进行匹配。 在这个例子中, "customers" 和 "orders" 是文字描述部分,必须完全匹配, "{customerId}" 是变量部分。
2.HTTP Request 属性
Web API 在选择action时,会基于HTTP的request类型的。
默认的,Web API 会匹配controller方法的开始部分,例如方法名称为PostCustomers 会匹配HTTP的Post request。
我们也可以通过方法修饰属性来控制request类型。
方法修饰属性 :[HttpDelete] [HttpGet] [HttpHead] [HttpOptions] [HttpPatch] [HttpPost] [HttpPut]
[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }
以上代码控制HTTP的request类型为Post.
3.HTTP 方法列表属性 AcceptVerbs
[Route("api/books")]
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public void MakeCollection() { }
以上代码同时支持Get和Post.
4.路由前缀属性 Route Prefixes
[RoutePrefix("api/books")]
public class BooksController : ApiController
{// GET api/books[Route("")]public IEnumerable<Book> Get() { ... }// GET api/books/5[Route("{id:int}")]public Book Get(int id) { ... }// POST api/books[Route("")]public HttpResponseMessage Post(Book book) { ... }
}
用符号“~”覆盖路由前缀
[RoutePrefix("api/books")]
public class BooksController : ApiController
{// GET /api/authors/1/books[Route("~/api/authors/{authorId:int}/books")]public IEnumerable<Book> GetByAuthor(int authorId) { ... }// ...
}
5 路由约束
通过路由约束来约束参数类型。语法{参数:字段类型}
[Route("users/{id:int}")]
public User GetUserById(int id) { ... }[Route("users/{id:int:min(1)}")]
public User GetUserById(int id) { ... }//有默认值
public class BooksController : ApiController
{[Route("api/books/locale/{lcid:int=1033}")]public IEnumerable<Book> GetBooksByLocale(int lcid) { ... }
}
支持的约束类型:
Constraint | Description | Example |
---|---|---|
alpha | Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) | {x:alpha} |
bool | Matches a Boolean value. | {x:bool} |
datetime | Matches a DateTime value. | {x:datetime} |
decimal | Matches a decimal value. | {x:decimal} |
double | Matches a 64-bit floating-point value. | {x:double} |
float | Matches a 32-bit floating-point value. | {x:float} |
guid | Matches a GUID value. | {x:guid} |
int | Matches a 32-bit integer value. | {x:int} |
length | Matches a string with the specified length or within a specified range of lengths. | {x:length(6)} {x:length(1,20)} |
long | Matches a 64-bit integer value. | {x:long} |
max | Matches an integer with a maximum value. | {x:max(10)} |
maxlength | Matches a string with a maximum length. | {x:maxlength(10)} |
min | Matches an integer with a minimum value. | {x:min(10)} |
minlength | Matches a string with a minimum length. | {x:minlength(10)} |
range | Matches an integer within a range of values. | {x:range(10,50)} |
regex | Matches a regular expression. | {x:regex(^\d{3}-\d{3}-\d{4}$)} |
6 路由名称
在HTTP 调用方法时,可以使用路由名称调用。
[Route("api/books/{id}", Name="GetBookById")]public BookDto GetBook(int id) {// Implementation not shown...}
ASP.NET CORE WebAPI 中 Route 属性配置相关推荐
- NET问答: 如何将 ASP.NET Core WebAPI 中抛出的异常封装成对象?
咨询区 rianjs: 在 ASP.NET Core WebAPI 中,我的 Controller 代码如下: [Route("create-license/{licenseKey}&quo ...
- 在 Asp.Net Core WebAPI 中防御跨站请求伪造攻击
什么是跨站请求伪造 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或 ...
- ASP.NET Core WebAPI中使用JWT Bearer认证和授权
为什么是 JWT Bearer ASP.NET Core 在 Microsoft.AspNetCore.Authentication 下实现了一系列认证, 包含 Cookie, JwtBearer, ...
- ASP.NET Core WebAPI中的分析工具MiniProfiler
安装 我们可以使用Nuget来下载这个包. PM> Install-Package MiniProfiler.AspNetCore.Mvc 配置Startup.cs MiniProfiler配置 ...
- ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必是件很痛苦的事情吧,但文档又必须写,而且文档的格式如果没有具体要求的话,最终完成的文档则完全取决于开发者 ...
- 【转】ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
原文链接:https://www.cnblogs.com/yilezhu/p/9241261.html 引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必 ...
- .NET Core WebApi中实现多态数据绑定
什么是多态数据绑定? 我们都知道在ASP.NET Core WebApi中数据绑定机制(Data Binding)负责绑定请求参数, 通常情况下大部分的数据绑定都能在默认的数据绑定器(Binder)中 ...
- Asp.Net Core WebAPI+PostgreSQL部署在Docker中
PostgreSQL是一个功能强大的开源数据库系统.它支持了大多数的SQL:2008标准的数据类型,包括整型.数值值.布尔型.字节型.字符型.日期型.时间间隔型和时间型,它也支持存储二进制的大对像,包 ...
- 第十三节:Asp.Net Core WebApi基础总结和请求方式-第十八节
一. 基础总结 1.Restful服务改造 Core下的WebApi默认也是Restful格式服务,即通过请求方式(Get,post,put,delete)来区分请求哪个方法,请求的URL中不需要写方 ...
最新文章
- vscode断开调试服务器文件,vscode显示等待调试器断开连接
- 下载源码,开源代码库
- maven 编译mybatis项目时xml文件无法编译到target目录下的解决方法
- BUUCTF--练习场-- basic--上传文件漏洞经典靶场upload-labs-- Pass1-3(Pass4简单尝试)
- postgresql 遍历字符串数组_每日一道编程题(348):1005.K次取反后最大化的数组和...
- 条款12:复制对象时勿忘其每一个部分
- MyBatis框架 拦截器简单使用
- c how to program; 习题:3.25 编写一个利用循环打印表格数据的程序.
- 二元函数偏导数公式_二阶偏导数公式详解
- 【Windows】无法访问指定设备,路径或文件,您可能没有合适的权限访问这个项目
- [feather]StarlingUi框架——初识feather、界面启动及Ui加载
- 如何在VSCode设置/取消隐藏文件
- 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 10.35 scre
- 利用Xshell映射云端服务器的visdom,进行训练过程可视化
- 黑马程序员——java语言基础部分——网络编程
- 安装 AWS Load Balancer Controller 附加组件
- 建立Baseline之repo,manifest
- vue 存取、设置、清除cookie
- 七夕python小礼物
- Etherscan以太坊浏览器中Input Data解析方法
热门文章
- SpringBoot项目使用@Value读取配置文件application.yml的值
- 未来电竞旗舰:iQOO 8系列售价3799元起
- 2020年机器视觉,就业前景如何?
- 快速将网页内的公式粘贴到文档中
- 项目管理的五个典型工具
- 【2020年高被引学者】 陶大程 悉尼大学
- 计算机系统能及时处理过程,在( )操作系控制下,计算机系统能及时处理由过程控制反馈的数据并做出响应。...
- IBIS建模——第2部分:为何以及如何创建您自己的IBIS模型
- 40岁销售被裁员后抑郁了,学Python是他最后的希望
- mysql-8.0.31-winx64详细安装教程