// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.Mvc
{
///
/// A formatter which selects itself when content-negotiation has failed and writes a 406 Not Acceptable response.
///
public class HttpNotAcceptableOutputFormatter : IOutputFormatter
{
///
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
return context.FailedContentNegotiation ?? false;
}
///
public Task WriteAsync(OutputFormatterContext context)
{
var response = context.HttpContext.Response;
response.StatusCode = StatusCodes.Status406NotAcceptable;
return Task.FromResult(true);
}
}
}