C# 如何将XML输出格式化程序添加到ASP.Net样板文件中?

C# 如何将XML输出格式化程序添加到ASP.Net样板文件中?,c#,asp.net-core,asp.net-core-mvc,aspnetboilerplate,C#,Asp.net Core,Asp.net Core Mvc,Aspnetboilerplate,使用ASP.NET Core 2.2、ASP.NET样板文件版本4.1、Modul Zero Core版本4.4.0。 API接受JSON对象作为输入并返回JSON对象。我面临的任务是确保将XML与JSON结合使用的可能性。 我这样试过: // file RootNs.Web.Mvc Startup.cs public IServiceProvider ConfigureServices(IServiceCollection services) { // ... services

使用ASP.NET Core 2.2、ASP.NET样板文件版本4.1、Modul Zero Core版本4.4.0。 API接受JSON对象作为输入并返回JSON对象。我面临的任务是确保将XML与JSON结合使用的可能性。
我这样试过:

// file RootNs.Web.Mvc Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services) {
    // ...
    services.AddMvc(options => {

        options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());

        options.ReturnHttpNotAcceptable = true;
        options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
        options.RespectBrowserAcceptHeader = true;

        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
        // so also tried
        // options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());

        options.FormatterMappings
        .SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml"));
    })
}
//文件RootNs.Web.Mvc Startup.cs
公共IServiceProvider配置服务(IServiceCollection服务){
// ...
services.AddMvc(选项=>{
options.Filters.Add(新的AutoValidateAntiforgeryTokenAttribute());
options.ReturnHttpNotAcceptable=true;
options.OutputFormatters.RemoveType();
options.reserverBrowserAcceptHeader=true;
options.OutputFormatters.Add(新的XmlSerializerOutputFormatter());
//所以我也试过了
//添加(新的XmlDataContractSerializerOutputFormatter());
options.FormatterMappings
.SetMediaTypeMappingForFormat(“xml”,MediaTypeHeaderValue.Parse(“应用程序/xml”);
})
}
之后,我用FormatFilter属性在API中修饰该方法。但这没有帮助。

如何添加对XML输出格式化程序的支持?应在哪个项目(*.Web.Mvc或*.Web.Host)中编辑文件Startup.cs?

将其添加到宿主项目中。这是WebAPI exposerAdd,将其添加到宿主项目中。这是WebAPI公开程序