Ich möchte einen benutzerdefinierten Content-Type-Validierungsfilter implementieren, sodass ein benutzerdefiniertes Fehlermodell für einen nicht unterstützten Medientyp 415 bereitgestellt werden kann.Benutzerdefinierter Content-Type-Validierungsfilter?
Etwas wie folgt aus:
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;
}
}
}
}
Das Problem ist, dass die MVC-Pipeline "Fang" unsupported oder ungültigen Content-Type-Werte vor der Ausführung alle benutzerdefinierten Filter zu sein scheint. Selbst der Inhaltstyp "application/xml" wird abgelehnt.
Wo würde dies konfiguriert werden?
Meine MVC-Konfiguration besteht aus nicht viel mehr als das:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options =>
{
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 =>
{
options.Filters.Add(typeof(ValidateAntiForgeryTokenAttribute));
options.Filters.Add(typeof(ValidateContentTypeFilterAttribute));
options.Filters.Add(typeof(ValidateAcceptFilterAttribute));
options.Filters.Add(typeof(ValidateModelFilterAttribute));
});
...
}
Ergebnisfilter sind neu für mich! Das funktioniert perfekt - danke. – davenewza