开发者

Rewriting HTTP method with ASP .NET

开发者 https://www.devze.com 2023-02-16 19:45 出处:网络
Because most browsers only support HTTP GET and POS开发者_开发问答T it would be useful to have a HttpHandler that can rewrite the HTTP method.

Because most browsers only support HTTP GET and POS开发者_开发问答T it would be useful to have a HttpHandler that can rewrite the HTTP method.

The HTTP method would be set with a hidden field:

<form method="POST" action="...">
  <input type="hidden" name="_method" value="PUT">
  ...
</form>

If the user submits, a POST request is sent and an IHttpHandler should replace the requests HttpMethod, but it is a read-only property in .NET.

How can I rewrite the HTTP method in .NET?


In mvc (ver 2+) you can use the HtmlHelper.HttpMethodOverride helper, which creates a hidden input value with the required action method.

http://msdn.microsoft.com/en-us/library/ee402924.aspx

this creates this hidden input (for delete):
<input name="X-HTTP-Method-Override" type="hidden" value="DELETE" />

some more info:

http://geekswithblogs.net/michelotti/archive/2010/01/08/implementing-a-delete-link-with-mvc-2-and-httpmethodoverride.aspx

UPDATE:

Looking a bit deeper into how this works in the MVC pipeline it's actually MVC (ActionMethodSelectorAttribute, ActionInvoker,RedirectToRoute) that handles this and not the RouteModule like I thought before.

You can look it up in the MVC source (from codeplex)... It's quite strightforward. The more interesting parts are in HttpRequestBaseExtensions and HttpRequestExtensions


If you control the server side of the equation already, why not write a proxy server to do this for you? You could use something like FiddlerCore to do the heavy lifting.

It would be reusable across a wide range of applications and would not necessarily need to be specific to IIS and .NET.


You can use HttpContext.Current.Request.RequestType. This property is not read only and you can change it on the c#.

For more information, check this.

0

精彩评论

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