Routing in ASP.NET Web API
Why is HttpGet required only for some actions?
https://stackoverflow.com/questions/28068868/why-is-httpget-required-only-for-some-actions
Please refer to the post here
You will see that you can use naming convention (which is why the methods with Get in the name work), or you can explicitly specify the HTTP method for an action by decorating the action with the correct HTTP attribute.
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 verb, not the URI path, to select the action. You can also use MVC-style routing in Web API. This article does not assume any knowledge of ASP.NET MVC.
If you self-host Web API, you must set the routing table directly on the HttpSelfHostConfiguration
object. For more information, see Self-Host a Web API.
The reason for using "api" in the route is to avoid collisions with ASP.NET MVC routing. That way, you can have "/contacts" go to an MVC controller, and "/api/contacts" go to a Web API controller. Of course, if you don't like this convention, you can change the default route table.
Once a matching route is found, Web API selects the controller and the action:
- To find the controller, Web API adds "Controller" to the value of the {controller} variable.
- To find the action, Web API looks at the HTTP verb, and then looks for an action whose name begins with that HTTP verb name.
For example, with a GET request, Web API looks for an action prefixed with "Get", such as "GetContact" or "GetAllContacts".
This convention applies only to GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH verbs.
You can enable other HTTP verbs by using attributes on your controller. We'll see an example of that later.
- Other placeholder variables in the route template, such as {id}, are mapped to action parameters.
另外,如果route template定义如下,nameaction是自动对应到controller下的方法的。然后controller下的方法,是不需要显式地添加HttpPost的attribute的
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{action}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
There are two special placeholders: "{controller}" and "{action}".
- "{controller}" provides the name of the controller.
- "{action}" provides the name of the action. In Web API, the usual convention is to omit "{action}".
Routing in ASP.NET Web API的更多相关文章
- 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 ...
- 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 M ...
- 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- ...
- ASP.NET Web API中的Routing(路由)
[译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...
- ASP.NET Web Api
1.参考资料 Routing in Asp.NET Web Api: http://www.asp.net/web-api/overview/web-api-routing-and-actions/r ...
- Getting Started with ASP.NET Web API 2 (C#)
By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Com ...
- ASP.NET Web API系列教程目录
ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...
- 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API
Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...
- [翻译]ASP.NET Web API的路由
原文:Routing in ASP.NET Web API 在我们新建一个Web API项目时,会在App_Start文件夹下的WebApiConfig.cs中定义一个默认路由: config.Rou ...
随机推荐
- centos6.5下安装samba服务器与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html http://www.cnblogs.com/x_wukong/p/56 ...
- ilbc编解码在android实现
iLBC 是为专为提供稳健的 IP 语音通信而开发的语音 codec,以窄带语音为设计基础,具有 8 kHz 的采样率.iLBC codec 支持两种基本的帧长度:13.3 kbps 比特率下编码帧长 ...
- Delphi StringReplace – 替换字符函数
Delphi StringReplace – 替换字符函数 Delphi中的StringReplace函数是SysUtils单元中自带的函数,该函数可以替换字符串中的指定字符. 1 2 3 4 5 6 ...
- [LintCode] 两个排序数组的中位数
class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: ...
- Casperjs中fill提交表单遇到的问题
1.if you access internet with proxy please add --ignore-ssl-errors=true --ssl-protocol=a ...
- SpringMVC 运行流程以及与Spring 整合
1. 运行流程 2. Spring 和 SpringMVC 整合 // 1. 导入 jar 包 // 2. 配置 web.xml <!-- 配置 Spring 的核心监听器 --> < ...
- win7下docker配置加速器
1.docker-machine ssh default(有时可省略) 2.sudo sed -i "s|EXTRA_ARGS='|EXTRA_ARGS='--registry-mirror ...
- Scrapy框架-scrapy框架快速入门
1.安装和文档 安装:通过pip install scrapy即可安装. Scrapy官方文档:http://doc.scrapy.org/en/latest Scrapy中文文档:http://sc ...
- (转)专项:Android 内存泄露实践分析
今天看到一篇关于Android 内存泄露实践分析的文章,感觉不错,讲的还算详细,mark到这里. 原文发表于:Testerhome: 作者:ycwdaaaa ; 原文链接:https://teste ...
- php socket 处理只是来数据流,该怎样避免(好像是堵塞了)
php socket 处理只是来数据流,该怎样处理(好像是堵塞了) 需求:php接受一个硬件往8888port上发送数据,假设收到后,应socket_send函数返回"\xFA\x01\x0 ...