Setting up JsonOutputFormatter with AddMvcCore
I am trying to set up an ASP.NET Core thin pipeline and am using AddMvcCore
instead AddMvc
. I copied this mostly from AddMvc , omitting the Razor related items:
public void ConfigureServices(IServiceCollection services) =>
services
.AddMvcCore(
options =>
{
// JsonOutputFormatter not found in OutputFormatters!
var jsonOutputFormatter =
options.OutputFormatters.OfType<JsonOutputFormatter>.First();
jsonOutputFormatter.SupportedMediaTypes.Add("application/ld+json");
})
.AddApiExplorer()
.AddAuthorization()
.AddFormatterMappings()
.AddRazorViewEngine()
.AddRazorPages()
.AddCacheTagHelper()
.AddDataAnnotations()
.AddJsonFormatters()
.AddCors();
I want to add a MIME type to JsonOutputFormatter
, but it cannot be found in the collection OutputFormatters
. However, the application can still output JSON, so I think this JsonOutputFormatter
is being added later. How can I get the JsonOutputFormatter?
+3
source to share
2 answers
public void ConfigureServices(IServiceCollection services) =>
services
.AddMvcCore(
options =>
{
var jsonOutputFormatter = options.OutputFormatters.FirstOrDefault(p => p.GetType() == typeof(JsonOutputFormatter)) as JsonOutputFormatter;
// Then add
jsonOutputFormatter?.SupportedMediaTypes.Add("application/ld+json");
})
.AddApiExplorer()
.AddAuthorization()
.AddFormatterMappings()
.AddRazorViewEngine()
.AddRazorPages()
.AddCacheTagHelper()
.AddDataAnnotations()
.AddJsonFormatters()
.AddCors();
+1
source to share