您好,我在配置/测试时遇到问题 带有 .NET Framework 4.8 的 Swashbuckle/Swagger 5.6.0(ASP.NET [不是 ASP.NET Core!])
从 WebUI Swagger 测试 GET 方法会引发错误。
对邮递员做同样的事情是:
内容类型:“application/json”-好的
内容类型:“application/ocet-stream”-同样的错误
响应正文错误:
Message: "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
ExceptionMessage: "No MediaTypeFormatter is available to read an object of type 'SomeParams' from content with media type 'application/octet-stream'.",
ExceptionType: "System.Net.Http.UnsupportedMediaTypeException",
StackTrace: " w System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n w System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
看起来缺少请求内容类型标头存在问题,我无法在 SwaggerWebUI 中正确设置它。
ASP.NET Swashbuckle 中没有 Produces / Consumes 属性,因此我按照以下说明手动添加了它们(customattribute + customfilters + 在 SwaggerConfig.cs 中注册): https://blog.kloud.com.au/2017/08/04/swashbuckle-pro-tips-for-aspnet-web-api-part-1/ 但它不起作用,并且参数内容类型组合框没有出现。
我还尝试过编辑WebApiConfig.cs,使用/不使用全局/普通格式化程序添加/清除SupportedMediaTTypes等。
//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
var mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
formatter.SupportedMediaTypes.Clear();
formatter.SupportedMediaTypes.Add(mediaType);
config.Formatters.Clear();
config.Formatters.Add(formatter);
var jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
jsonFormatter.SupportedMediaTypes.Clear();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
控制器代码示例:
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MainClass
{
public class xxx_V1Controller : ApiController
{
[HttpGet]
[SwaggerConsumes("application/json")]
[SwaggerProduces("application/json")]
[Route("api/xxx_V1/GetAll/", Name = "xxx_V1_GetAll")]
public Rxxx_V1 GetAll(SomeParamsXyz)
{
return AuthorizationController.MainClass.yyy_GetAll_V1(SomeParams xyz);
}
[HttpGet]
[Route("api/xxx_V1/GetAll2/", Name = "xxx_V1_GetAll2")]
public Rxxx_V1 GetAll2(SomeParams xyz)
{
return AuthorizationController.MainClass.yyy_GetAll2_V1(SomeParams xyz);
}
}
}
一些参数类
public class SomeParams : IXyz, IFilters
{
public string XXX { get; set; }
public bool GetCountOnly { get; set; }
public bool YYY{ get; set; }
public LoadOptions Options { get; set; }
public List<Filter> Filters { get; set; }
}
http://localhost:20220/swagger/docs/v1 片段:
"/api/xxx_V1/GetAll": {
"get": {
"tags": [
"xxx_V1"
],
"operationId": "xxx_V1_GetAll",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "options.xxx",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.filters",
"in": "query",
"required": false,
"type": "array",
"items": {},
"collectionFormat": "multi"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/xxxx.Objects.Rxxx_V1"
}
}
}
}
},
自定义过滤器消费/生产可以正常工作,因为没有这些属性的所有其他 Get 方法也具有带有 application/xml 的 json。
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
我发现一个网站指出 Swashbuckler 团队决定不支持使用 Body 进行 GET 调用。我尝试了上述所有解决方案,但没有一个对我有用。我的解决方案是将操作更改为 POST。抱歉,我没有讨论的链接。但 GitHub 将其报告为 Bug,但已关闭。 https://github.com/swagger-api/swagger-ui/issues/5388