Created
February 17, 2012 03:03
-
-
Save pedroreys/1850200 to your computer and use it in GitHub Desktop.
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 NotAcceptableConnegHandler : DelegatingHandler | |
{ | |
private const string allMediaTypesRange = "*/*"; | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
var acceptHeader = request.Headers.Accept; | |
if (!acceptHeader.Any(x => x.MediaType == allMediaTypesRange)) | |
{ | |
var hasFormetterForRequestedMediaType = GlobalConfiguration | |
.Configuration | |
.Formatters | |
.Any(formatter => acceptHeader.Any(mediaType => formatter.SupportedMediaTypes.Contains(mediaType))); | |
if (!hasFormetterForRequestedMediaType) | |
return Task<HttpResponseMessage>.Factory.StartNew(() => new HttpResponseMessage(HttpStatusCode.NotAcceptable)); | |
} | |
return base.SendAsync(request, cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment