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

  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] 标记方法。

web api :Routing in ASP.NET Web API的更多相关文章

  1. Routing in ASP.NET Web API

    Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...

  2. 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 ...

  3. 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 ...

  4. ASP.NET Web API 框架研究 ASP.NET Web API 路由

    ASP.NET Web API 核心框架是一个独立的.抽象的消息处理管道,ASP.NET Web API有自己独立的路由系统,是消息处理管道的组成部分,其与ASP.NET路由系统有类似的设计,都能找到 ...

  5. 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】

    原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...

  6. 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- ...

  7. Adding the Test API in The ASP.NET Web API Help Page

    1.通过NuGet引用Web API Test Client 引用玩该DLL会生成如下文件: 这里面就是我们的帮助文档界面 2.在项目属性中进行如下设置,勾选XMl文档文件,并设置路径 3.在项目的A ...

  8. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  9. ASP.NET Web API 路由对象介绍

    ASP.NET Web API 路由对象介绍 前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了 ...

随机推荐

  1. July 9th, Week 28th Saturday, 2016

    Every cloud has a silver lining. 山穷水尽疑无路,柳暗花明又一村. Every cloud has a silver lining, that just because ...

  2. 【转】深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p

    一.可能的组合: (1)const char*p (2)char const*p (3)char *const p(4)const char **p (5)char const**p (6)char ...

  3. Rabbitmq实现负载均衡与消息持久化

      Rabbitmq 是对AMQP协议的一种实现.使用范围也比较广泛,主要用于消息异步通讯. 一,默认情况下Rabbitmq使用轮询(round-robin)方式转发消息.为了较好实现负载,可以在消息 ...

  4. Android 添加cookie

    /** * 添加cookie * * @param url */ private void setCookie(String url) { // 获取uid String uid = UserData ...

  5. Extjs ComboBox 动态选中第一项

    有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...

  6. 【PHP绘图技术&&验证码绘制】

    PHP绘图是比较简单的事情,基本绘图如直线.圆.矩形.弧线.填充矩形.填充扇形.非中文字的打印.中文文字的打印在在下面的代码中会纤细讲解. 需要支持中文的字体,可以到windows自带的字体库中找,并 ...

  7. GitHub 中国区前 100 名到底是什么样的人?

    本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...

  8. [LeetCode] Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  9. Oracle 创建/删除 表空间、用户、授权

    首先以DBA连接到数据库:sqlplus / as sysdba; --创建表空间 create tablespace test_tablespace datafile 'D:\developer\o ...

  10. android 入门-Service

    sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...