如何以XML而不是JSON显示SwaggerResponse [英] How to display SwaggerResponse in XML instead of JSON
本文介绍了如何以XML而不是JSON显示SwaggerResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Swagger UI中以XML格式而不是JSON显示响应.我怎样才能做到这一点?这是我要修改的代码:
I want to display a response in Swagger UI in XML format instead of JSON. How I can achieve this? This is the code I want to adapt:
[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);
}
推荐答案
您可以尝试使用属性 SwaggerProducesAttribute
装饰方法,如
You could try decorating the method with an attribute SwaggerProducesAttribute
as described here:
[SwaggerProduces("text/xml")]
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(FeedModel))]
public async Task<IActionResult> GetCompanyPostFeed(Guid companyId)
为了不喜欢仅链接的答案,我将在此处重述该文章的一些相关内容:
In keeping with the dislike of link-only answers, I'll reproduce some of the relevant bits of that article here:
[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中,您将需要以下内容:
Then in SwaggerConfig.cs, you'll need something like:
config
.EnableSwagger(c =>
{
...
c.OperationFilter<ProducesOperationFilter>();
...
}
这篇关于如何以XML而不是JSON显示SwaggerResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文