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这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了 ...
随机推荐
- 3DS MAX调慢摄像机动画
在3ds max的右下角找到时间配置,然后出现该对话框,然后调整结束时间,将原来的时间翻倍,就能够是摄像机动画变慢. 下图的旋转速度比上图慢一半.
- Android中通过反射获取资源Id
package com.cp.utils; import android.content.Context; public class CPResourceUtil { public static in ...
- Cocoapods的安装报错 - Error installing pods:activesupport requires Ruby version >=2.2.2
1.打开终端 2 移除现有 Ruby 默认源 输入以下指令 $gem sources --remove https://rubygems.org/ 3.使用新的源 输入以下指令 $gem source ...
- 20145206邹京儒《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
20145206<Java程序设计>实验报告一:Java开发环境的熟悉(Windows+IDEA) 实验内容及步骤 1.使用JDK编译.运行简单的Java程序: 建立实验目录: 在IDEA ...
- js 倒计时点击和当前时间
<input id="btn" type="submit" value="确定" disabled="disabled&qu ...
- javascript - DOM对象控制HTML元素详解
1.方法 getElementsByName() -- 获取name getElementByTagName() -- 获取 getAttribute() --获取元素属性 se ...
- Java 解析XML的几种方法
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便. XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已. 基本的解析方式 ...
- 全局压缩http响应头
见代码: public class CompressAttribute : ActionFilterAttribute { public override void OnActionExecuting ...
- 重温WCF之构建一个简单的WCF(一)(1)通过控制台和IIS寄宿服务
一.理解什么是WCFWCF就是.NET平台下各种分布式技术的集成,并提供了一套统一的编程接口 二.WCF的定义WCF(Windows Communication Foundation)是微软为构建面向 ...
- UML- 模型图介绍
第一类 用例图 第二类 静态图 类图 对象图 包图第三类 行为图 状态图 活动图第四类 交互图 序列图 协助图第五类 实现图 构件图 部署图 1 用例图:从用户角度描述系统功能,以及每个系统功 ...