开发者

Advantages of WebInvoke POST compared to WEBGET

开发者 https://www.devze.com 2023-03-08 07:13 出处:网络
hi i found one of the examples of wcf REST with a WEBINVOKE method just like the following [OperationContract]

hi i found one of the examples of wcf REST with a WEBINVOKE method just like the following

[OperationContract]
[WebInvoke(
BodyStyle=WebMessageBodyStyle.Bare,
Method="POST",
RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")]
int Insert(string StudentName, string Chair, string AverageNote);


[OperationContract]
[WebGet(
BodyStyle= WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
Student[] GetAllStudents();

my question is can i use WEBGET method instead of WEBINVOKE just like below and what exactly is the difference betwenn WEBINVOKE POST and WEBGET, as per my observation we are sending the parameters by appending query strings in the URI Templates for both WEbGet 开发者_高级运维and WebInvoke POST, what are the advantages that we can get using WebInvoke POST which we can not get using WEBGET

[OperationContract]
[WebGet(
BodyStyle=WebMessageBodyStyle.Bare, 
RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")]
int Insert(string StudentName, string Chair, string AverageNote);


It is very big difference. First of all REST is usually used with these HTTP verbs:

  • GET - retrieving items
  • POST - inserting items
  • PUT - updating items
  • DELETE - deleting items

You should never use GET for anything else then retrieving items. Using HTTP GET for data modification is considered as a bad practice in whole web development. To trigger GET you just need to create link on the web page or simply type a URL to the browser. You will hit refresh 50 times and you have 50 same inserts. Data modification should be always done with POST. If you have form which triggers HTTP POST (Post cannot be triggered directly) and you hit refresh browser will usually ask you if you want the form to be submitted again = if you really want to post and process the data again to the server.

Another problem is that GET request can be cached and redirected but POST requests cannot.


This link should provide further insight into the answers provided:

http://blog.markkoltnuk.com/2011/02/14/understanding-wcf-webinvokewebget-attributes/

Lets explain, once and for all, what the difference between WebInvoke & WebGet.

WebGet (Commonly used to retrieve data)

The WebGet attribute exposes operations using the GET verb. You can access the endpoint is directly via a Web browser by typing the URI to the service into the address bar. Parameters can be sent within the URI either as query string parameters or embedded in the URI. The WebGet attribute should be used only for data retrieval due to its caching capabilities.

WebInvoke (Commonly used for data input/update) The WebInvoke attribute exposes services using other HTTP verbs such as POST, PUT, and DELETE. POST is the default value but it can be changed by setting the Method property of the attribute. The WebInvoke attribute should be used only for data input/update.


To answer your question i would recommend you to understand the HTTP protocol semantics, especially HTTP Verbs, such as GET, POST, PUT, DELETE
HTTP GET is done to retrieve resource from any location and therefore the request should not alter the state of the resource.
HTTP POST is used to create and sometimes update content and hence has been used in the Insert method above.
If you run the WCF service above and see how are these request formed and server you would see that GET does not have a body payload whereas POST has. In case of POST the body contains the content that needs to be created\update.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号