Skip to content

Instantly share code, notes, and snippets.

@ajtowf
Created April 30, 2013 15:41
Show Gist options
  • Save ajtowf/5489546 to your computer and use it in GitHub Desktop.
Save ajtowf/5489546 to your computer and use it in GitHub Desktop.
Webapi MethodOverrideHandler
public class MethodOverrideHandler : DelegatingHandler
{
private const string Header = "X-HTTP-Method-Override";
private readonly string[] methods = { "DELETE", "HEAD", "PUT" };
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Post && request.Headers.Contains(Header))
{
var method = request.Headers.GetValues(Header).FirstOrDefault();
if (method != null && methods.Contains(method, StringComparer.InvariantCultureIgnoreCase))
{
request.Method = new HttpMethod(method);
}
}
return base.SendAsync(request, cancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment