Asp.net 使用AddMvcCore配置JsonOutputFormatter
我正在尝试设置一个精简的ASP.NET核心管道,并使用Asp.net 使用AddMvcCore配置JsonOutputFormatter,asp.net,asp.net-mvc,asp.net-core,asp.net-core-mvc,Asp.net,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,我正在尝试设置一个精简的ASP.NET核心管道,并使用AddMvcCore而不是AddMvc。我主要是从中复制的,省略了与剃须刀相关的项目: public void ConfigureServices(IServiceCollection services) => services .AddMvcCore( options => { // JsonOutputFormatter
AddMvcCore
而不是AddMvc
。我主要是从中复制的,省略了与剃须刀相关的项目:
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();
public void配置服务(IServiceCollection服务)=>
服务
.AddMvcCore(
选项=>
{
//在OutputFormatters中找不到JsonOutputFormatter!
var jsonOutputFormatter=
options.OutputFormatters.OfType.First();
jsonOutputFormatter.SupportedMediaTypes.Add(“应用程序/ld+json”);
})
.AddApiExplorer()
.AddAuthorization()
.AddFormatterMappings()
.AddRazorViewEngine()
.AddRazorPages()
.AddCacheTagHelper()
.AddDataAnnotations()
.AddJsonFormatters()文件
.AddCors();
我想向
JsonOutputFormatter
添加MIME类型,但在OutputFormatters
集合中找不到它。这就是说,应用程序仍然可以输出JSON,因此我认为JsonOutputFormatter
将在稍后添加。如何获取JsonOutputFormatter?您可以在添加JSON格式化程序后,在末尾使用addmvcopions
(位置很重要)访问JSON格式化程序:
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();
services
.AddMvcCore()
.AddJsonFormatters()
.AddMvcOptions(options => { // do your work here });
如果通过添加MIME类型将JsonOutputFormatter添加到输出格式化程序中,它是否具有您所追求的效果,或者是否被覆盖?