如何使用SwashBuckle设置或删除默认响应内容类型 [英] How do I set or remove the Default Response Content Type Using SwashBuckle

查看:80
本文介绍了如何使用SwashBuckle设置或删除默认响应内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The default response content type when using SwashBuckle is text/plain. How can I change the default to be application/json or even remove text/plain?

推荐答案

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

The response content of an end point is not determined by Swashbuckle but by the formatters set in the configuration of your ASP.NET Web API project.

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

To remove the text/plain content type and only support application\json you could add this to the Register method of your WebApiConfig:

GlobalConfiguration.Configuration.Formatters.Clear();

var jsonFormatter = new JsonMediaTypeFormatter();
jsonFormatter.SupportedMediaTypes.Clear();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue('application\json'));

GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);

这篇关于如何使用SwashBuckle设置或删除默认响应内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆