如何使用 SwashBuckle 设置或删除默认响应内容类型

Muh*_*eed 6 asp.net-web-api swashbuckle

使用 SwashBuckle 时的默认响应内容类型是text/plain。如何将默认值更改为application/json甚至删除text/plain

ven*_*rik 6

端点的响应内容不是Swashbuckle由 ASP.NET Web API 项目配置中设置的格式化程序决定,而是由该格式化程序决定。

要删除text/plain内容类型并仅支持application/json您可以将其添加到Register您的方法中WebApiConfig

GlobalConfiguration.Configuration.Formatters.Clear();

var jsonFormatter = new JsonMediaTypeFormatter();
jsonFormatter.SupportedMediaTypes.Clear();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);
Run Code Online (Sandbox Code Playgroud)