我有一个API,它可以返回csv或json,在Accept头mimetype上进行降级。
我用[Produces("application/json","text/csv")]装饰了这个方法。这里是一个带有ASP.NET核心默认项目的MRE:
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}[HttpGet(Name = "GetWeatherForecast")]
[Produces("application/json","text/csv")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = "Freezing"
})
.ToArray();
}这使得Swagger中的mimetype选择成为可能,并且它被正确地添加到请求的Accept头中。我的问题是,示例响应值的格式不是csv,而是json。

如何配置如何为自定义mimetype(如text/csv )呈现示例?
我使用的是Swashbuckle.AspNetCore版本6.3.1。
发布于 2022-04-29 13:51:42
https://stackoverflow.com/questions/72034441
复制相似问题