Created
February 5, 2014 10:21
-
-
Save feanz/8d8f3e14601641ce9513 to your computer and use it in GitHub Desktop.
Way to use the method override header to route traffic to controller actions.
This file contains 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 | |
{ | |
readonly string[] _methods = { "DELETE", "HEAD", "PUT" }; | |
const string _header = "X-HTTP-Method-Override"; | |
protected override Task<HttpResponseMessage> SendAsync( | |
HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
// Check for HTTP POST with the X-HTTP-Method-Override header. | |
if (request.Method == HttpMethod.Post && request.Headers.Contains(_header)) | |
{ | |
// Check if the header value is in our methods list. | |
var method = request.Headers.GetValues(_header).FirstOrDefault(); | |
if (_methods.Contains(method, StringComparer.InvariantCultureIgnoreCase)) | |
{ | |
// Change the request method. | |
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