与 WEBGET 相比,WebInvoke POST 的优势 [英] Advantages of WebInvoke POST compared to WEBGET

查看:47
本文介绍了与 WEBGET 相比,WebInvoke POST 的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 wcf REST 的示例之一,使用 WEBINVOKE 方法,如下所示

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();

我的问题是我可以像下面一样使用 WEBGET 方法而不是 WEBINVOKE,WEBINVOKE POST 和 WEBGET 之间到底有什么区别,根据我的观察,我们通过在 WEBGet 和 WebInvoke POST 的 URI 模板中附加查询字符串来发送参数,使用 WebInvoke POST 可以获得哪些优势,而使用 WEBGET 则无法获得

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);

推荐答案

差别很大.首先,REST 通常与这些 HTTP 动词一起使用:

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

  • GET - 检索项目
  • POST - 插入项目
  • PUT - 更新项目
  • DELETE - 删除项目

除了检索项目之外,您永远不应该将 GET 用于其他任何事情.使用 HTTP GET 进行数据修改在整个 Web 开发中被认为是一种不好的做法.要触发 GET,您只需在网页上创建链接或只需在浏览器中输入 URL.您将点击刷新 50 次,并且您有 50 个相同的插入.数据修改应始终使用 POST 完成.如果您有触发 HTTP POST 的表单(无法直接触发 Post)并且您点击刷新浏览器通常会询问您是否希望再次提交表单 = 如果您真的想再次向服务器发布和处理数据.

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.

另一个问题是 GET 请求可以缓存和重定向,但 POST 请求不能.

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

这篇关于与 WEBGET 相比,WebInvoke POST 的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆