web api :Routing in ASP.NET Web API
引
Web API 和SignalR都是在服务层。

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing.
The main difference is that Web API uses the HTTP method, not the URI path, to select the action.(web api和mvc路由最主要的区别是,mvc是使用URI路径来选择action的,而web api 则是使用http方法来选择action的。)
You can also use MVC-style routing in Web API. This article does not assume any knowledge of ASP.NET MVC.
参考官网:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
1.路由表
1.1默认配置
默认的路由映射,所以,调用api的时候,必须以api开头
- routes.MapHttpRoute(
- name: "API Default",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
eg:
- /api/contacts
- /api/contacts/1
- /api/products/gizmo1
而 /contacts/1就不会成功。
1.2默认约定
默认的约定,以Get,Delete,Put,Post这些开头的方法:
- public class ProductsController : ApiController
- {
- public void GetAllProducts() { }
- public IEnumerable<Product> GetProductById(int id) { }
- public HttpResponseMessage DeleteProduct(int id){ }
- }
|
HTTP Method |
URI Path |
Action |
Parameter |
|
GET |
api/products |
GetAllProducts |
(none) |
|
GET |
api/products/4 |
GetProductById |
|
|
DELETE |
api/products/4 |
DeleteProduct |
|
|
POST |
api/products |
(no match) |
2.路由变量
2.1 Http方法
使用HttpGet, HttpPut, HttpPost, or HttpDelete 来进行标记方法。
也可以使用[AcceptVerbs("GET", "HEAD")]来标记多个选择。
2.2使用ActionName来路由
- routes.MapHttpRoute(
- name: "ActionApi",
- routeTemplate: "api/{controller}/{action}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
则以上的配置可以支持两种:
One is
- public class ProductsController : ApiController
- {
- [HttpGet]
- public string Details(int id);
- }
The other is:(api/products/thumbnail/id)
- public class ProductsController : ApiController
- {
- [HttpGet]
- [ActionName("Thumbnail")]
- public HttpResponseMessage GetThumbnailImage(int id);
- [HttpPost]
- [ActionName("Thumbnail")]
- public void AddThumbnailImage(int id);
- }
2.3非Action
使用[NonAction] 标记方法。
web api :Routing in ASP.NET Web API的更多相关文章
- Routing in ASP.NET Web API
Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...
- Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)
http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...
- Routing in ASP.NET Web API和配置文件的设定读取
Routing Tables In ASP.NET Web API, a controller is a class that handles HTTP requests. The public me ...
- ASP.NET Web API 框架研究 ASP.NET Web API 路由
ASP.NET Web API 核心框架是一个独立的.抽象的消息处理管道,ASP.NET Web API有自己独立的路由系统,是消息处理管道的组成部分,其与ASP.NET路由系统有类似的设计,都能找到 ...
- 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】
原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...
- Create a REST API with Attribute Routing in ASP.NET Web API 2
原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute- ...
- Adding the Test API in The ASP.NET Web API Help Page
1.通过NuGet引用Web API Test Client 引用玩该DLL会生成如下文件: 这里面就是我们的帮助文档界面 2.在项目属性中进行如下设置,勾选XMl文档文件,并设置路径 3.在项目的A ...
- ASP.NET Web API中的Routing(路由)
[译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...
- ASP.NET Web API 路由对象介绍
ASP.NET Web API 路由对象介绍 前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了 ...
随机推荐
- 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。
问题补充: “/Source”应用程序中的服务器错误. 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或 ...
- 在Linux中安装JDK的步骤
相信不少学习Java的朋友都在Windows操作系统中安装过JDK,这里就不对JDK做详细的介绍了. 在Windows下安装JDK可参考:JDK的安装和配置 1.下载JDK 我们可以去官网(http: ...
- kmp
#include <bits/stdc++.h> #define MAXN 100000 using namespace std; string a, b; int next[MAXN]; ...
- 图文转换——NABCD
我们小组要做的是一款可以对文字进行扫描转为txt格式将文字保存下来的移动通信终端的APP. N-need 生活中经常遇到这种问题,看到报纸上的一篇文章,特别想收藏这篇文章,或者是在网上看到一篇好 ...
- NYOJ题目842整除的尾数
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsUAAAIMCAIAAACSTkYzAAAgAElEQVR4nO3dO3KjzBrG8bMJ5VqIYx ...
- 很少有人会告诉你的Android开发基本常识
原文:很少有人会告诉你的Android开发基本常识. 文章介绍了一些关于开发.测试.版本管理.工具使用等方面的知识.
- Ubuntu 14.04 Trusty安装java环境
原文:Install Oracle Java 6, 7, or 8 in Ubuntu 14.04 Trusty 命令如下: sudo add-apt-repository ppa:webupd8te ...
- 昨晚把家里的ie升级到11
其实网上有些东西是实用的,不过之前的一次锁屏唤醒机器死机我就强制关机了,昨天把大部分驱动升级.
- 查看局域网内在线的主机ip和mac地址
]# nmap -sP Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-12 22:43 CST Nmap scan report for 192. ...
- 【翻译二十】-java线程池
Thread Pools Most of the executor implementations in java.util.concurrent use thread pools, which co ...