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 ...
随机推荐
- ryu启动问题总结
在Mininet中启动ryu控制器,首先切换到ryu中的app目录下: cd ryu/ryu/app 启动ryu: ryu-manager simple_switch.py 遇到了如下的错误提示: 这 ...
- rabbitmq 3.6.11 centos 7 安装
http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm http://www.rabbitmq.co ...
- Spring Security OAuth2 授权失败(401) 问题整理
Spring Cloud架构中采用Spring Security OAuth2作为权限控制,关于OAuth2详细介绍可以参考 http://www.ruanyifeng.com/blog/2014/0 ...
- Flutter入门之无状态组件
Flutter核心理念 flutter组件采用函数式响应框架构建,它的灵感来自于React.它设计的核心思想是组件外构建UI,简单解释一下就是组件鉴于它当前的配置和状态来描述它的视图应该是怎样的,当组 ...
- Android异步处理之AsyncTaskLoader简单使用
简介 不管是在Android应用开发还是Android平台开发中,异步处理通常是最基本的coding要求.如果你还在主线程中写一些数据库,网络请求,读写本地文件等操作的话那说明你还不是一个合格的And ...
- JS去遍历Table的所有单元格中的内容
用JS去遍历Table的所有单元格中的内容,可以用如下JS代码实现 function GetInfoFromTable(tableid) { var tableInfo = ""; ...
- python基础之类的isinstance与issubclass、反射
一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo: pass o ...
- jquery获取浏览器类型和版本号的方法
$(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="Mi ...
- vue-cli注册全局组件
在main.js开头引入组件,然后注册组件,例如: import Vue from 'vue' import VueRouter from 'vue-router' import VueResourc ...
- django博客项目2.建立 Django 博客应用
建立博客应用 我们已经建立了 Django 博客的项目工程,并且成功地运行了它.不过到目前为止这一切都还只是 Django 为我们创建的项目初始内容,Django 不可能为我们初始化生成博客代码,这些 ...