如何注释我的ASP.NET WebAPI操作,以便swagger元数据包含我的资源支持的内容类型?
具体来说,我希望文档能够显示我的资源之一可以返回“原始”的application/json和application/xml,但现在也返回一种新的格式,application/vnd.blah+json或+xml。
发布于 2016-02-01 11:32:51
您需要做的是:Swagger spec:您需要将您的响应类型添加到该操作的响应类型列表中:
"produces": [
"application/json",
"text/json"
],这可以用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")]注释进行设置。
然后在operationsFilter中应用swaggerConfig.cs:
c.OperationFilter<CustomResponseType>();注意:与operation.operationId == "myCustomName"不同,您基本上可以在特定的路由或其他任何情况下这样做。ApiDescription提供了很多关于上下文的信息。
发布于 2016-04-01 05:42:11
在Controller的API方法上,您可以使用下面的代码来设置如下属性:
[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)]
public HttpResponseMessage GetAuthorityForm(string id)
{
....注意:'Exclusive=true‘将删除所有其他内容类型,否则使用新属性将在Swagger下拉列表中添加一个新的响应内容类型。它不会修改Controller或API,只修改文档。
SwaggerConfig.cs
GlobalConfiguration.Configuration
.EnableSwagger(c =>
// Set filter to apply Custom Content Types to operations
//
c.OperationFilter<ResponseContentTypeOperationFilter>();SwaggerResponseContentTypeAttribute.cs
/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class SwaggerResponseContentTypeAttribute : Attribute
{
/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
/// <param name="responseType"></param>
public SwaggerResponseContentTypeAttribute(string responseType)
{
ResponseType = responseType;
}
/// <summary>
/// Response Content Type
/// </summary>
public string ResponseType { get; private set; }
/// <summary>
/// Remove all other Response Content Types
/// </summary>
public bool Exclusive { get; set; }
}ResponseContentTypeOperationFilter.cs
public class ResponseContentTypeOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>().FirstOrDefault();
if (requestAttributes != null)
{
if (requestAttributes.Exclusive)
operation.produces.Clear();
operation.produces.Add(requestAttributes.ResponseType);
}
}
}发布于 2017-03-01 10:12:55
@OzBob's answer假设您只想向方法添加一个属性。如果要为同一方法添加和记录多个内容类型,可以使用以下方法:
SwaggerResponseContentTypeAttribute.cs
/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerResponseContentTypeAttribute : Attribute
{
/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
/// <param name="responseType"></param>
public SwaggerResponseContentTypeAttribute(string responseType)
{
ResponseType = responseType;
}
/// <summary>
/// Response Content Type
/// </summary>
public string ResponseType { get; private set; }
/// <summary>
/// Remove all other Response Content Types
/// </summary>
public bool Exclusive { get; set; }
}ResponseContentTypeOperationFilter.cs
public class ResponseContentTypeOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>();
foreach (var requestAttribute in requestAttributes)
{
if (requestAttribute.Exclusive)
{
operation.produces.Clear();
}
operation.produces.Add(requestAttribute.ResponseType);
}
}
}注意,当您在同一方法上有多个属性并且要替换现有的内容类型时,应该只在第一个属性上设置Exclusive = true。否则,您将不会将所有属性都放入文档中。
https://stackoverflow.com/questions/34990291
复制相似问题