asp web api 怎么使用put和delete。
Method Overriding RESTful services allow the clients to act on the resources through methods such as GET, POST, PUT, DELETE, and so on. GET and POST are the most frequently used methods. Most of the corporate firewalls allow port 80, the typical port of HTTP. However, some do have restrictions in terms of the HTTP methods allowed. GET and POST methods are very common, but others such as DELETE can be disallowed. The X-HTTP-Method-Override header can help you work around this problem. A typical solution involving this header is to send X-HTTP-Method-Override in the request with the actual verb intended (DELETE or PUT) and submit the request using POST; that is, the request line with the dummy POST verb tricks the firewall into allowing the request. In ASP.NET Web API, a message handler, such as the one shown in Listing 4-2, can replace POST with the method specified in X-HTTP-Method-Override. The message handler runs early in the pipeline and is the best extensibility point suitable for this purpose.
Request Line
Request Headers
GET /home.html HTTP/1.1 Accept: text/html User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) Host: server.com [Blank line indicating the end of request headers]
Figure 4-4. Request message
www.it-ebooks.info
Chapter 4 ■ http anatomy and SeCurity
45
Listing 4-2. Method Override
public class MethodOverrideHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Post && request.Headers.Contains("X-HTTP-Method-Override"))
{
var method = request.Headers.GetValues("X-HTTP-Method-Override").FirstOrDefault();
bool isPut = String.Equals(method, "PUT", StringComparison.OrdinalIgnoreCase);
bool isDelete = String.Equals(method, "DELETE", StringComparison.OrdinalIgnoreCase);
if (isPut || isDelete) { request.Method = new HttpMethod(method); }
}
return await base.SendAsync(request, cancellationToken);
}
}
To test the preceding MethodOverrideHandler, you will need a tool like Fiddler, covered in depth later in this chapter. Fiddler is useful in capturing and analyzing HTTP traffic. Also, it lets you hand-code a request complete with request headers and send it to an endpoint with an HTTP method of your choice. Figure 4-5 illustrates how you can make a POST request with an X-HTTP-Method-Override header set to PUT. If MethodOverrideHandler is plugged into the pipeline by making an entry in WebApiConfig.cs file under App_Start, this request will invoke the PUT action method in the controller instead of POST.
HTTP Response The HTTP response has the status line as the first line of the response. As shown in Figure 4-6, the status line starts with the HTTP version, followed by a space, followed by the status code and a space, and then the reason phrase. The request line is terminated by a CR and an LF character.
Figure 4-5. Fiddler Composer
asp web api 怎么使用put和delete。的更多相关文章
- 创建包含CRUD操作的Web API接口5:实现Delete方法
本节是前面四节的延续,在前面几节中我们创建了Web API并添加了必要的基础设施,实现了Get.Post.和Put方法.本节中,我们将介绍如何在Web API中实现Delete方法. 在RESTful ...
- [整理]IIS 6.0 下部署 Asp.net MVC Web Api 后 HTTP PUT and DELETE 请求失败
http://guodong.me/?p=1560 ASP.NET MVC 4 has a new feature called WebAPI which makes it much easier t ...
- asp web api json 序列化后 把私有字段信息也返回了解决办法
serialization returns private properties Are your types marked as [Serializable]? Serializable means ...
- ASP.NET Web API 接口执行时间监控
软件产品常常会出现这样的情况:产品性能因某些无法预料的瓶颈而受到干扰,导致程序的处理效率降低,性能得不到充分的发挥.如何快速有效地找到软件产品的性能瓶颈,则是我们感兴趣的内容之一. 在本文中,我将解释 ...
- Asp.Net Web API VS Asp.Net MVC
http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...
- 开始一个简单的ASP.NET Web API 2 (C#)
创建一个Web API 项目 在本教程中,你将使用ASP.NET Web API 来创建一个web API 并返回产品列表. 网页前端使用jQuery 显示结果. 选择ASP.NET Web Appl ...
- Web API 配置Help Page
当你创建一个web API,它通常用于创建一个帮助页面,以便其他开发人员知道如何调用你的API.你可以手动创建所有的文档,但最好是autogenerate尽可能多. 简化这个任务,ASP.Web AP ...
- 如果调用ASP.NET Web API不能发送PUT/DELETE请求怎么办?
理想的RESTful Web API采用面向资源的架构,并使用请求的HTTP方法表示针对目标资源的操作类型.但是理想和现实是有距离的,虽然HTTP协议提供了一系列原生的HTTP方法,但是在具体的网络环 ...
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我们对资源的CRUD操作都是通过相应的Http方法来实现——Post(新 ...
随机推荐
- poj -1065 Wooden Sticks (贪心or dp)
http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...
- 加密解密(5)SSL形象比喻
转自: http://blog.csdn.net/cloverphp/article/details/11737433 前言: 关于公钥,私钥请看前几篇文章 SSL 协议既用到了公钥加密技术(握手 ...
- 【Android】 PopupWindow使用小结
PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法: 1.PopupWindow中的listview无响应 这个主要是因为show写在了set ...
- IE下easyui 缓存问题
$.ajaxSetup ({ cache: false //关闭AJAX相应的缓存 }); 这一句话就足够了,很管用!
- open_binary_frm
参数uchar* head 是已经分配好内存的64个字节的地址 http://mysql.taobao.org/monthly/2015/08/07/ /** *先从.frm文件读取64字节 *第28 ...
- datagridview的某些属性以及增删改查
private void button1_Click(object sender, EventArgs e) //查询 { dataGridView1.AutoGenerateColumns = fa ...
- POJ 2828 (线段树 单点更新) Buy Tickets
倒着插,倒着插,这道题是倒着插! 想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0. 从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的 ...
- UVa 10791 Minimum Sum LCM【唯一分解定理】
题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...
- Need to add a caption to a jpg, python can't find the image
importImage,ImageDraw,ImageFont, os, glob list = glob.glob('*.jpg')for infile in list:print infile f ...
- MVC+Ef项目(3) 抽象数据库访问层的统一入口;EF上下文线程内唯一
抽象一个数据库访问层的统一入口(类似于EF的上下文,拿到上下文,就可以拿到所有的表).实际这个入口就是一个类,类里面有所有的仓储对应的属性.这样,只要拿到这个类的实例,就可以点出所有的仓储,我们在 R ...