diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/UnsupportedContentTypeFilter.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/UnsupportedContentTypeFilter.cs
index 0c134d7dc9..fab0cbe68f 100644
--- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/UnsupportedContentTypeFilter.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/UnsupportedContentTypeFilter.cs
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
@@ -10,8 +11,16 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// and short-circuits the pipeline
/// with an Unsupported Media Type (415) response.
///
- public class UnsupportedContentTypeFilter : IActionFilter
+ public class UnsupportedContentTypeFilter : IActionFilter, IOrderedFilter
{
+ ///
+ /// Gets or sets the filter order. .
+ ///
+ /// Defaults to -3000 to ensure it executes before .
+ ///
+ ///
+ public int Order { get; set; } = -3000;
+
///
public void OnActionExecuting(ActionExecutingContext context)
{
@@ -32,7 +41,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
foreach (var kvp in modelState)
{
var errors = kvp.Value.Errors;
- for (int i = 0; i < errors.Count; i++)
+ for (var i = 0; i < errors.Count; i++)
{
var error = errors[i];
if (error.Exception is UnsupportedContentTypeException)
diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs
index b27a549321..cfb5df0887 100644
--- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs
@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
+using System.Text;
using System.Threading.Tasks;
using BasicWebSite.Models;
using Newtonsoft.Json;
@@ -58,6 +59,38 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
);
}
+ [Fact]
+ public async Task ActionsReturnUnsupportedMediaType_WhenMediaTypeIsNotSupported()
+ {
+ // Arrange
+ var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/contact")
+ {
+ Content = new StringContent("some content", Encoding.UTF8, "text/css"),
+ };
+
+ // Act
+ var response = await Client.SendAsync(requestMessage);
+
+ // Assert
+ await response.AssertStatusCodeAsync(HttpStatusCode.UnsupportedMediaType);
+ }
+
+ [Fact]
+ public async Task ActionsReturnUnsupportedMediaType_WhenEncodingIsUnsupported()
+ {
+ // Arrange
+ var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/contact")
+ {
+ Content = new StringContent("some content", Encoding.UTF7, "application/json"),
+ };
+
+ // Act
+ var response = await Client.SendAsync(requestMessage);
+
+ // Assert
+ await response.AssertStatusCodeAsync(HttpStatusCode.UnsupportedMediaType);
+ }
+
[Fact]
public async Task ActionsReturnBadRequest_UsesProblemDescriptionProviderAndApiConventionsToConfigureErrorResponse()
{