原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

属性路由,attribute routing,是web api 2 提供的,而早期的称为约定路由, convention-based routing。在web api 2中,两种可以共存。

以下是原文的思维导图总结:

代码片段:

怎么启用属性路由?

  1. public static class WebApiConfig
  2. {
  3.     public static void Register(HttpConfiguration config)
  4.     {
  5.         // Web API 配置和服务
  6.         // 将 Web API 配置为仅使用不记名令牌身份验证。
  7.         config.SuppressDefaultHostAuthentication();
  8.         config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
  9.  
  10.         // Web API 路由
  11.         config.MapHttpAttributeRoutes();   //此句为关键
  12.  
  13.         config.Routes.MapHttpRoute(
  14.             name: "DefaultApi",
  15.             routeTemplate: "api/{controller}/{id}",
  16.             defaults: new { id = RouteParameter.Optional}
  17.         );
  18.     }
  19. }

通过HTTP METHOD和Route属性一起使用

  1. [Route("api/books")]
  2. [HttpPost]
  3. public HttpResponseMessage CreateBook(Book book) { ... }

路由前缀Attribute,用于简化公共部分

  1. [RoutePrefix("api/books")]
  2. public class BooksController : ApiController
  3. {
  4.     // GET api/books
  5.     [Route("")]
  6.     public IEnumerable<Book> Get() { ... }
  7.  
  8.     // GET api/books/5
  9.     [Route("{id:int}")]
  10.     public Book Get(int id) { ... }
  11.  
  12.     // POST api/books
  13.     [Route("")]
  14.     public HttpResponseMessage Post(Book book) { ... }
  15. }

参数约束

  1. [Route("users/{id:int:min(1)}")]
  2. public User GetUserById(int id) { ... }

可选参数

  1. public class BooksController : ApiController
  2. {
  3.     [Route("api/books/locale/{lcid:int?}")]
  4. //[Route("api/books/locale/{lcid:int=1033}")]
  5.     public IEnumerable<Book> GetBooksByLocale(int lcid = 1033) { ... }
  6. }

为路由指定一个名称

[Route("api/books/{id}", Name="GetBookById")]

// Generate a link to the new book and set the Location header in the response.

string uri = Url.Link("GetBookById", new { id = book.BookId });

WebApi 2:属性路由 [Route()],attribute routing的更多相关文章

  1. Web API 路由 [二] Attribute Routing

    1) 启用.在App_Start - WebApiConfig.cs下 //在Register函数添加如下代码: config.MapHttpAttributeRoutes(); 2) 使用.Cont ...

  2. WebApi(三)-属性路由 自定义访问路径

    启用属性路由: 1.在WebApiConfig中加入如下代码: //属性路由 config.MapHttpAttributeRoutes();

  3. ASP.NET Web API 中 特性路由(Attribute Routing) 的重名问题

    刚才忘了说了,在控制器名重名的情况下,特性路由是不生效的.不然的话就可以利用特性路由解决同名的问题了. 而且这种不生效是真的不生效,不会提示任何错误,重名或者什么的,直接会报告404,所以也是个坑.

  4. MVC5 属性路由

    属性路由(attribute routing)是最新被引进到MVC5中的,它和传统路由共同组成了mvc5中的两种主要路由. 1. 高质量的url应该满足以下几点 域名便于记忆和拼写 简短 便于输入 可 ...

  5. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 题外话:由于这个技术点是新学的,并不属于原系列,但借助了原系列的项目背景,故命名外传系列,以后也可 ...

  6. WebApi2官网学习记录---Attribute Routing

    从WebApi 1迁移到WebAPI 2要改变配置代码如下: WebApi 1: protected void Application_Start() { // WARNING - Not compa ...

  7. Attribute Routing

    Attribute Routing 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 题外话:由于这个技术点是新学的,并不属于原系列,但借助了原系列 ...

  8. Web API中的路由(二)——属性路由

    一.属性路由的概念 路由让webapi将一个uri匹配到对应的action,Web API 2支持一种新类型的路由:属性路由.顾名思义,属性路由使用属性来定义路由.通过属性路由,我们可以更好地控制We ...

  9. ASP.NET Web API 2中的属性路由(Attribute Routing)

    如何启用属性路由并描述属性路由的各种选项? Why Attribute Routing? Web API的第一个版本使用基于约定的路由.在这种类型的路由中,您可以定义一个或多个路由模板,这些模板基本上 ...

随机推荐

  1. storyboard pushViewController 的时候,新的界面黑屏

    storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...

  2. java中带继承类的加载顺序详解及实战

    一.背景: 在面试中,在java基础方面,类的加载顺序经常被问及,很多时候我们是搞不清楚到底类的加载顺序是怎么样的,那么今天我们就来看看带有继承的类的加载顺序到底是怎么一回事?在此记下也方便以后复习巩 ...

  3. MongoDB配置文件YAML-based选项全解

    配置文件部分 MongoDB引入一个YAML-based格式的配置文件.2.4版本以前的仍然兼容. 我的mongodb配置文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  4. velocity思维导图笔记

  5. EasyUi – 6.easyui常见问题

    1.进度条 2.JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 3. 三张表的连接查询现在到datagrid里 4.日期组合框DateBox设置readonly ...

  6. BlueTooth: 蓝牙基础知识进阶——链路控制操作

    转自:http://blog.csdn.net/augusdi/article/details/25887395 七链路控制操作 链路控制操作就是用来描述一个设备是如何加入piconet又是如何从一个 ...

  7. PHP define()的用法

    define()函数理解1(着重于作用的理解) define() 函数定义一个常量. 常量的特点: 常量类似变量,不同之处在于:在设定以后,常量的值无法更改常量名,不需要开头的美元符号 ($),作用域 ...

  8. 【JAVA单例模式详解】

    设计模式是一种思想,适合于任何一门面向对象的语言.共有23种设计模式. 单例设计模式所解决的问题就是:保证类的对象在内存中唯一. 举例: A.B类都想要操作配置文件信息Config.java,所以在方 ...

  9. python中多线程与非线程的执行性能对比

    此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  10. go sample - hello world

    入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...