I am building a WEB API project and it seems like I am unable to remove the default of Swagger defaulting to XML formatting. I tried removing the AddXmlSerializerFormatters method call but my controller responses fail at that point. Anyone have a simple way to eliminate all XML in Web API and Swagger and only support JSON? Below is my current code in 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;
});
Controller code:
[HttpGet]
[Route("/companies")]
[ValidateModelState]
[SwaggerOperation("GetCompanies")]
public virtual IActionResult GetCompanies()
{
ICompanyRepository _companyRespository = new CompaniesRepository();
List<Company> companies = _companyRespository.GetAll();
return Ok(companies);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…