问题描述
如何注释ASP.NET WebAPI操作,以使庞大的元数据包含资源支持的内容类型?
How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support?
具体来说,我希望文档显示我的资源之一可以返回原始" application/json
和application/xml
,但现在还可以返回新格式,application/vnd.blah+json
或+xml
.
Specifically, I want the documentation to show that one of my resources can return the 'original' application/json
and application/xml
but also now returns a new format, application/vnd.blah+json
or +xml
.
推荐答案
您需要执行的操作是;昂扬的规格:您需要将响应类型添加到该操作的响应类型列表中
What you need to do is this;Swagger spec:you need to add your response-type to the list of response-types for that operation
"produces": [
"application/json",
"text/json"
],
这可以通过OperationFilter来完成
This can be done with an OperationFilter
收到伪代码!
public class CustomResponseType : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.operationId == "myCustomName")
{
operation.produces.Add("application/vnd.blah+json");
}
}
}
OperationId可以通过[SwaggerOperation("myCustomName")]
注释设置.
the OperationId can be set through the [SwaggerOperation("myCustomName")]
annotation.
然后在swaggerConfig.cs中应用OperationsFilter
Then apply the operationsFilter in the swaggerConfig.cs
c.OperationFilter<CustomResponseType>();
注意:代替operation.operationId == "myCustomName"
您可以针对特定路线或其他任何方式进行操作. ApiDescription提供了大量有关上下文的信息.
Note:instead of operation.operationId == "myCustomName"
you could do it for a particular route or anything else basically. ApiDescription gives ALOT of info about context.
这篇关于Swashbuckle Swagger-如何注释内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!