Swagger - 如何删除 XML 作为格式化选项;只需要 JSON 格式

Swagger - how to remove XML as a formatting option; only want JSON formatting

我正在构建一个 WEB API 项目,似乎我无法删除 Swagger 默认为 XML 格式的默认设置。我尝试删除 AddXmlSerializerFormatters 方法调用,但此时我的控制器响应失败。谁有简单的办法,把WebAPI和Swagger中的XML全部去掉,只支持JSON?下面是我在 Startup.cs

中的当前代码
services.AddMvc(options =>
                {
                    options.InputFormatters.RemoveType<Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter>();
                    options.OutputFormatters.RemoveType<Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter>();
                }).AddXmlSerializerFormatters();

            services.AddControllers().AddJsonOptions(options => {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                options.JsonSerializerOptions.DictionaryKeyPolicy = null;

            });

控制器代码:

[HttpGet]
        [Route("/companies")]
        [ValidateModelState]
        [SwaggerOperation("GetCompanies")]
        public virtual IActionResult GetCompanies()
        {
            ICompanyRepository _companyRespository = new CompaniesRepository();
            List<Company> companies = _companyRespository.GetAll();
            return Ok(companies);
        }

你的控制器有 [Produces("application/json")] 属性吗 类?

来自Get started with Swashbuckle and ASP.NET Core

Mark the model with attributes, found in the System.ComponentModel.DataAnnotations namespace, to help drive the Swagger UI components.

Add the [Produces("application/json")] attribute to the API controller. Its purpose is to declare that the controller's actions support a response content type of application/json.

The Response Content Type drop-down selects this content type as the default.