Created
April 30, 2013 15:41
-
-
Save ajtowf/5489546 to your computer and use it in GitHub Desktop.
Webapi MethodOverrideHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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