原文: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. FTL标签

    <#if blockObject ??> <#else> </if>判断对象是否存在 <#if componentid ?? &&compon ...

  2. 3ds max输出图片

    通过渲染菜单调出改窗口,然后调整成这种模式,就能渲染出这种效果的图. 可以调背景色: 全局照明:染色,这个控制的是渲染物体的颜色

  3. 关于each

    1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...

  4. http://poj.org/problem?id=3278(bfs)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 ...

  5. windows重新获取IP

    win+r------->cmd------>ipconfig /release (释放ip) ipconfig /renew  重新获取ip

  6. Delphi测试线程的时间

    在16位时代,当我们在Windows3.x下编程时,经常会用到GetTickCount()或者timeGetTime()来判断一段代码的执行时间.示例如下 var StartTime, Total: ...

  7. codeforce好地方啊 Bear and Elections *

    Codeforces Round #318 居然可以看测试数据,哪里没过一目了然,哈哈哈 #include<cstdio> #include<iostream> #includ ...

  8. [Linux] 安装JDK和Maven及hadoop相关环境

    紧接上一篇,继续安装hadoop相关环境 JDK安装:     1. 下载,下面这两个地址在网上找的,可以直接下载:         http://download.oracle.com/otn-pu ...

  9. 源码方式安装mysql5.5

    mysql5.5开始,源码配置编译工具configure变成了cmake,所以先要去把cmake装上.并安装make,bison,cmake,gcc-c++,ncurses的包 去http://www ...

  10. SercureCRT无法正常连接Ubuntu14.0.4.1的解决办法

    问题描述 通过VirtualBox重新安装了ubuntu 14.0.4.1 虚拟服务器,在SercureCRT中使用root帐号连接ubuntu14.0.4.1的时候,提示“Password Auth ...