如何在 XML 而不是 JSON 中显示 SwaggerResponse
How to display SwaggerResponse in XML instead of JSON
我想在 Swagger UI 中以 XML 格式而不是 JSON 显示响应。我怎样才能做到这一点?这是我要改编的代码:
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(FeedModel))]
public async Task<IActionResult> GetCompanyPostFeed(Guid companyId)
{
var feed = new FeedModel();
// Some database requests
return Content(feed, "text/xml", Encoding.UTF8);
}
您可以按照 here:
中所述尝试使用属性 SwaggerProducesAttribute
修饰方法
[SwaggerProduces("text/xml")]
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(FeedModel))]
public async Task<IActionResult> GetCompanyPostFeed(Guid companyId)
由于不喜欢仅 link 的答案,我将在此处复制该文章的一些相关内容:
[AttributeUsage(AttributeTargets.Method)]
public class SwaggerProducesAttribute : Attribute
{
public SwaggerProducesAttribute(params string[] contentTypes)
{
this.ContentTypes = contentTypes;
}
public IEnumerable<string> ContentTypes { get; }
}
public class ProducesOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var attribute = apiDescription.GetControllerAndActionAttributes<SwaggerProducesAttribute>().SingleOrDefault();
if (attribute == null)
{
return;
}
operation.produces.Clear();
operation.produces = attribute.ContentTypes.ToList();
}
}
然后在 SwaggerConfig.cs 中,您需要这样的东西:
config
.EnableSwagger(c =>
{
...
c.OperationFilter<ProducesOperationFilter>();
...
}
我想在 Swagger UI 中以 XML 格式而不是 JSON 显示响应。我怎样才能做到这一点?这是我要改编的代码:
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(FeedModel))]
public async Task<IActionResult> GetCompanyPostFeed(Guid companyId)
{
var feed = new FeedModel();
// Some database requests
return Content(feed, "text/xml", Encoding.UTF8);
}
您可以按照 here:
中所述尝试使用属性SwaggerProducesAttribute
修饰方法
[SwaggerProduces("text/xml")]
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(FeedModel))]
public async Task<IActionResult> GetCompanyPostFeed(Guid companyId)
由于不喜欢仅 link 的答案,我将在此处复制该文章的一些相关内容:
[AttributeUsage(AttributeTargets.Method)]
public class SwaggerProducesAttribute : Attribute
{
public SwaggerProducesAttribute(params string[] contentTypes)
{
this.ContentTypes = contentTypes;
}
public IEnumerable<string> ContentTypes { get; }
}
public class ProducesOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var attribute = apiDescription.GetControllerAndActionAttributes<SwaggerProducesAttribute>().SingleOrDefault();
if (attribute == null)
{
return;
}
operation.produces.Clear();
operation.produces = attribute.ContentTypes.ToList();
}
}
然后在 SwaggerConfig.cs 中,您需要这样的东西:
config
.EnableSwagger(c =>
{
...
c.OperationFilter<ProducesOperationFilter>();
...
}