Implicit verbs from a method name

If I create a webApi controller and populate it with methods prefixed with http-verbs, Api can correctly indicate which verb should be used on this controller.

 public class TestController : ApiController { public string GetData() { return "Called Get Method"; } public string PostData() { return "Called Post Method"; } public string PutData() { return "Called Put Method"; } } 

If you replace Post with Update , the Post method continues to work implicitly.

 public string UpdateData() { return "Called Updated Method"; } 

Is there a list of possible method prefixes and which verb they belong to? Also, is it possible to define custom prefixes? For example, if I wanted to always display a method starting with β€œSearch” in Post , can I define this?

+4
source share
2 answers

Implicit verbs are a function of the built-in routing and cannot be expanded manually.

This Asp.Net details the specific rules around implicit routing.

HTTP methods. The structure selects only the actions corresponding to the HTTP request method, defined as follows:

  • You can specify an HTTP method with the attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost or HttpPut.
  • Otherwise, if the controller method name begins with "Get", "Post", "Put", "Delete", "Head", "Options" or "Patch", the action supports this HTTP method.
  • If none of the above, the method supports POST.

The reason the UpdateData method works is because any method that is implicitly detected automatically by Post

+2
source

If you put your Routing as follows:

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); 

Consider routeTemplate . Now you can call action in the controller by name, and the prefix problem must exist. This approach is very useful if you have several actions on your controller with similar HTTP Verbs (say multiple GET or POST ).

+2
source

All Articles