title: Custom Content-Type validation filter?
body: <p>I want to implement a custom Content-Type validation filter so that a custom error model on a 415 Unsupported Media Type can be provided.  </p>
  
  <p>Something like this:</p>
  
  <pre><code>public class ValidateContentTypeFilterAttribute : ActionFilterAttribute
  {
      private const string JsonMimeType = "application/json";
  
      public override void OnActionExecuting(ActionExecutingContext context)
      {
          string requestMethod = context.HttpContext.Request.Method.ToUpper();
  
          if (requestMethod == WebRequestMethods.Http.Post || requestMethod == WebRequestMethods.Http.Put)
          {
              if (request.ContentType != JsonMimeType)
              {
                  // "Unsupported Media Type" HTTP result.
                  context.Result = new HttpUnsupportedMediaTypeResult();
                  return;
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem is that the MVC pipeline seems to be "catching" unsupported or invalid Content-Type values before executing any custom filters.  Even the 'application/xml' content type will be refused. </p>
  
  <p><strong>Where would this be configured?</strong></p>
  
  <p>My MVC configuration consists of not much more than this:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services
          .AddMvc()
          .AddJsonOptions(options =&gt;
          {
              options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
              options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
              options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
              options.SerializerSettings.Converters.Add(new SquidJsonConverter());
          })
          .AddMvcOptions(options =&gt;
          {
              options.Filters.Add(typeof(ValidateAntiForgeryTokenAttribute));
              options.Filters.Add(typeof(ValidateContentTypeFilterAttribute));
              options.Filters.Add(typeof(ValidateAcceptFilterAttribute));
              options.Filters.Add(typeof(ValidateModelFilterAttribute));
          });
      ...
  }
  </code></pre>