diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/InputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/InputFormatter.cs index f743faec66..32470fb09a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/InputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/InputFormatter.cs @@ -62,6 +62,15 @@ public virtual bool CanRead(InputFormatterContext context) return false; } + if (SupportedMediaTypes?.Count == 0) + { + var message = Resources.FormatFormatter_NoMediaTypes( + nameof(OutputFormatter), + nameof(SupportedMediaTypes)); + + throw new InvalidOperationException(message); + } + var contentType = context.HttpContext.Request.ContentType; if (string.IsNullOrEmpty(contentType)) { @@ -169,6 +178,15 @@ public IReadOnlyList GetSupportedContentTypes(string contentType, Type o return null; } + if (SupportedMediaTypes?.Count == 0) + { + var message = Resources.FormatFormatter_NoMediaTypes( + nameof(OutputFormatter), + nameof(SupportedMediaTypes)); + + throw new InvalidOperationException(message); + } + if (contentType == null) { // If contentType is null, then any type we support is valid. diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/OutputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/OutputFormatter.cs index 3b1a14c7c6..aa04773087 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/OutputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/OutputFormatter.cs @@ -79,6 +79,15 @@ public virtual IReadOnlyList GetSupportedContentTypes( string contentType, Type objectType) { + if (SupportedMediaTypes?.Count == 0) + { + var message = Resources.FormatFormatter_NoMediaTypes( + nameof(OutputFormatter), + nameof(SupportedMediaTypes)); + + throw new InvalidOperationException(message); + } + if (!CanWriteType(objectType)) { return null; @@ -166,6 +175,15 @@ public virtual bool CanWriteResult(OutputFormatterCanWriteContext context) throw new ArgumentNullException(nameof(context)); } + if (SupportedMediaTypes?.Count == 0) + { + var message = Resources.FormatFormatter_NoMediaTypes( + nameof(OutputFormatter), + nameof(SupportedMediaTypes)); + + throw new InvalidOperationException(message); + } + if (!CanWriteType(context.ObjectType)) { return false; diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs index 65d7e0b324..017ef555b5 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs @@ -1098,6 +1098,22 @@ internal static string FormatHtmlGeneration_ValueMustBeNumber(object p0) return string.Format(CultureInfo.CurrentCulture, GetString("HtmlGeneration_ValueMustBeNumber"), p0); } + /// + /// No media types found in '{0}.{1}'. Add at least one media type to the list of supported media types. + /// + internal static string Formatter_NoMediaTypes + { + get { return GetString("Formatter_NoMediaTypes"); } + } + + /// + /// No media types found in '{0}.{1}'. Add at least one media type to the list of supported media types. + /// + internal static string FormatFormatter_NoMediaTypes(object p0, object p1) + { + return string.Format(CultureInfo.CurrentCulture, GetString("Formatter_NoMediaTypes"), p0, p1); + } + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx b/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx index 9bb383b191..95a1b30f3e 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx +++ b/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx @@ -1,17 +1,17 @@  - @@ -331,4 +331,7 @@ The field {0} must be a number. + + No media types found in '{0}.{1}'. Add at least one media type to the list of supported media types. + \ No newline at end of file