首页 技术 正文
技术 2022年11月6日
0 收藏 386 点赞 969 浏览 1696 个字

Web API 和SignalR都是在服务层。

web api :Routing in ASP.NET Web API

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开头

  1. routes.MapHttpRoute(
  2.     name: “API Default”,
  3.     routeTemplate: “api/{controller}/{id}”,
  4.     defaults: new { id = RouteParameter.Optional }
  5. );

eg:

  • /api/contacts
  • /api/contacts/1
  • /api/products/gizmo1

而 /contacts/1就不会成功。

1.2默认约定

默认的约定,以Get,Delete,Put,Post这些开头的方法:

  1. public class ProductsController : ApiController
  2. {
  3.     public void GetAllProducts() { }
  4.     public IEnumerable<Product> GetProductById(int id) { }
  5.     public HttpResponseMessage DeleteProduct(int id){ }
  6. }

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来路由

  1. routes.MapHttpRoute(
  2.     name: “ActionApi”,
  3.     routeTemplate: “api/{controller}/{action}/{id}”,
  4.     defaults: new { id = RouteParameter.Optional }
  5. );

则以上的配置可以支持两种:

One is

  1. public class ProductsController : ApiController
  2. {
  3.     [HttpGet]
  4.     public string Details(int id);
  5. }

The other is:(api/products/thumbnail/id

  1. public class ProductsController : ApiController
  2. {
  3.     [HttpGet]
  4.     [ActionName(“Thumbnail”)]
  5.     public HttpResponseMessage GetThumbnailImage(int id);
  6.  
  7.     [HttpPost]
  8.     [ActionName(“Thumbnail”)]
  9.     public void AddThumbnailImage(int id);
  10. }

2.3非Action

使用[NonAction] 标记方法。

上一篇: Js+XML 操作
下一篇: String练习
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898