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的更多相关文章

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

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

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

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

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

  5. ASP.NET Web Api

    1.参考资料 Routing in Asp.NET Web Api: http://www.asp.net/web-api/overview/web-api-routing-and-actions/r ...

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

  7. ASP.NET Web API系列教程目录

    ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...

  8. 【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 ...

  9. [翻译]ASP.NET Web API的路由

    原文:Routing in ASP.NET Web API 在我们新建一个Web API项目时,会在App_Start文件夹下的WebApiConfig.cs中定义一个默认路由: config.Rou ...

随机推荐

  1. UIWebview打开.txt文件中文乱码解决

    用UIWebview打开txt文件有时候会出现乱码的情况,这种情况应该是txt的编码问题,解决方案如下: txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt. ...

  2. 【BZOJ3160】万径人踪灭 Manacher+FFT

    [BZOJ3160]万径人踪灭 Description Input Output Sample Input Sample Output HINT 题解:自己想出来1A,先撒花~(其实FFT部分挺裸的) ...

  3. jQuery选择器概览

    层级 ancestor descendant parent > child prev + next prev ~ siblings 基本筛选器 :first :not(selector) :ev ...

  4. mysql 中 select中 用case

    将 countertype 整数类型转成字符串类型 SELECT counterType, CASE counterType WHEN 1 THEN 'CTP'WHEN 2 THEN 'NULL'WH ...

  5. SQL 将列转成字符串并用逗号分隔

    SELECT STUFF((SELECT ',' + FieldName FROM TableName FOR XML PATH('')),1,1,'') AS T 其中的逗号可以换成其它字符 转换完 ...

  6. servlet 文件下载

    [本文简介] 一个servlet 文件下载 的简单例子. [文件夹结构] [java代码] package com.zjm.www.servlet; import java.io.BufferedIn ...

  7. 01 javaSe 01 抽象类和接口

      抽象类 接口   目录(?)[-] 1 抽象类与接口是面向对象思想层面概念不是程序设计语言层面概念 2 抽象类是本体的抽象接口是行为的抽象 3 C中抽象类与接口的探讨     目录(?)[+]   ...

  8. Linux学习笔记之文件权限

    前言: 说起文件权限,大家在windows下应该很熟悉就对文件右键属性,然后配置一点什么读写之类的权限,然后可以分配到每个的人. 对于linux 我先为大家介绍一个使用者和组和其他的概念说明一下 文件 ...

  9. PAT 1126 Eulerian Path[欧拉路][比较]

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  10. s5_day4作业

    # #流程控制练习题: # #==========>基础部分 # #练习一: # if True or False and False: # print('yes') # else: # pri ...