Skip to content

Instantly share code, notes, and snippets.

@pedroreys
Created February 17, 2012 03:03
Show Gist options
  • Save pedroreys/1850200 to your computer and use it in GitHub Desktop.
Save pedroreys/1850200 to your computer and use it in GitHub Desktop.
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