首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯巴什巴克尔摇摆-如何注释内容类型?

问斯巴什巴克尔摇摆-如何注释内容类型?
EN

Stack Overflow用户
提问于 2016-01-25 10:28:04
回答 4查看 29.8K关注 0票数 22

如何注释我的ASP.NET WebAPI操作,以便swagger元数据包含我的资源支持的内容类型?

具体来说,我希望文档能够显示我的资源之一可以返回“原始”的application/json和application/xml,但现在也返回一种新的格式,application/vnd.blah+json或+xml。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-02-01 11:32:51

您需要做的是:Swagger spec:您需要将您的响应类型添加到该操作的响应类型列表中:

代码语言:javascript
复制
"produces": [
            "application/json",
            "text/json"
            ],

这可以用OperationFilter来完成。

伪码传入!

代码语言:javascript
复制
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:

代码语言:javascript
复制
c.OperationFilter<CustomResponseType>();

注意:与operation.operationId == "myCustomName"不同,您基本上可以在特定的路由或其他任何情况下这样做。ApiDescription提供了很多关于上下文的信息。

票数 10
EN

Stack Overflow用户

发布于 2016-04-01 05:42:11

扩展@VisualBean's answer。

在Controller的API方法上,您可以使用下面的代码来设置如下属性:

代码语言:javascript
复制
[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)]
public HttpResponseMessage GetAuthorityForm(string id)
{
....

注意:'Exclusive=true‘将删除所有其他内容类型,否则使用新属性将在Swagger下拉列表中添加一个新的响应内容类型。它不会修改Controller或API,只修改文档。

SwaggerConfig.cs

代码语言:javascript
复制
 GlobalConfiguration.Configuration
            .EnableSwagger(c =>
 // Set filter to apply Custom Content Types to operations
 //
 c.OperationFilter<ResponseContentTypeOperationFilter>();

SwaggerResponseContentTypeAttribute.cs

代码语言:javascript
复制
/// <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

代码语言:javascript
复制
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);
        }
    }
}
票数 49
EN

Stack Overflow用户

发布于 2017-03-01 10:12:55

@OzBob's answer假设您只想向方法添加一个属性。如果要为同一方法添加和记录多个内容类型,可以使用以下方法:

SwaggerResponseContentTypeAttribute.cs

代码语言:javascript
复制
/// <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

代码语言:javascript
复制
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。否则,您将不会将所有属性都放入文档中。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34990291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档