c# - Why does ApiController require explicit Http verbs on methods? -
i'm using apicontroller
. struggle understand why apicontrollers differ controllers in ways.
take
public class questioncommentcontroller : apicontroller { questioncommentcrud crud = new questioncommentcrud(); // api/questioncomment/5 [httpget] public string read(int id)
i'm accustomed controller
types allow me create method without specifying legal verbs via attributes:
public class questioncommentcontroller : controller { questioncommentcrud crud = new questioncommentcrud(); // questioncomment/5 public string read(int id)
in latter case can perform get/post without specifying httpgetattribute
. find behavior confusing few reasons:
- there's 2
httpget
:system.web.http.httpget
,system.web.mvc.httpget
system.web.http.httpge
t required,system.web.mvc.httpget
not required requestsapicontroller
requests require unique route/api/controller...
controller
's allows me fall pit of success. newer apicontroller
requires hand holding.
i noticed default template has syntax don't understand:
public void post([frombody]string value) { }
the verb method name along funky [frombody]
thing going on. maybe why things setup way? assumptions exist usage of apicontroller
led design?
your api controllers don't require verbs on methods if follow built in convention. if prefix method names proper verb, get, post, etc. there's no need decorate attribute.
in case.
public string getread(int)
and live example project i'm working on
[authorize] public httpresponsemessage getstorelist([fromuri]namedviewmodel model)
no decoration necessary, system.web.http.httpget not required.
you can way have listed above or way have it. webapi allowing either rest or rpc style calls see fit. that's why see differences. inclusion of support restful style calls required additional work.
i agree 2 separate httpget attributes confusing, when both included in webapi project out of box. bit me few times when accidentally included wrong namespace.
Comments
Post a Comment