简体   繁体   English

使用多个SwaggerRespons的优雅方式

[英]Elegant way to use Multiple SwaggerResponses

I gonna use these attributes on my controller: 我将在控制器上使用以下属性:

    [SwaggerResponse((int)HttpStatusCode.OK, typeof(GetAutotopupConfigurationResponse))]
    [SwaggerResponse((int)HttpStatusCode.BadRequest, typeof(ErrorResponse), BadRequestMessage)]
    [SwaggerResponse((int)HttpStatusCode.Unauthorized, typeof(ErrorResponse), InvalidCredentiasMessage)]
    [SwaggerResponse((int)HttpStatusCode.Forbidden, typeof(ErrorResponse), UserNoRightsMessage)]
    [SwaggerResponse((int)HttpStatusCode.NotFound, typeof(ErrorResponse), AutopopupNotFoundMessage)]
    [SwaggerResponse((int)HttpStatusCode.InternalServerError, typeof(ErrorResponse), InternalServerErrorMessage)]

How do I simplify the logic and reduce code ammount or make it more flexible somehow? 如何简化逻辑并减少代码量或使其更灵活?

EDIT This answer applies to Asp.Net-Core but may be useful for this question too. 编辑此答案适用于Asp.Net-Core但也可能对该问题有用。

If you're using Swashbuckle you can use an IOperationFilter and Reflection to target specific endpoints and programmatically apply the responses. 如果您使用的是Swashbuckle ,则可以使用IOperationFilter和Reflection来定位特定的端点并以编程方式应用响应。

It's possible to use an IOperationFilter to apply InternalServerError to all endpoints in your service. 可以使用IOperationFilter将InternalServerError应用于服务中的所有端点。 Below is an example: 下面是一个示例:

public class ServerErrorResponseOperationFilter : IOperationFilter
    {            
        // Applies the specified operation. Adds 500 ServerError to Swagger documentation for all endpoints            
        public void Apply(Operation operation, OperationFilterContext context)
        {
            // ensure we are filtering on controllers
            if (context.MethodInfo.DeclaringType.BaseType.BaseType == typeof(ControllerBase)
                || context.MethodInfo.ReflectedType.BaseType == typeof(Controller))
            {
                operation.Responses.Add("500", new Response { Description = "Server Error" });
            }                        
        }
    }

You need to set Swagger to use these filters. 您需要设置Swagger才能使用这些过滤器。 You can do so by adding in the setup: 您可以通过添加设置来做到这一点:

services.AddSwaggerGen(swag =>
            {
                swag.SwaggerDoc("v1", new Info { Title = "Docs", Version = "v1" });

                // add swagger filters to document default responses
                swag.OperationFilter<ServerErrorResponseOperationFilter>();
            });

You can use other filters to apply 401 Unauthorized, 403 Forbidden, etc. You can even use Reflection to add 201 Created for actions decorated with [HttpPost] and you could do something similar for other Http attributes. 您可以使用其他过滤器来应用“ 401未经授权”,“ 403禁止”等。甚至可以使用“反射”添加201 Created为用[HttpPost]装饰的动作,并且可以对其他Http属性执行类似的操作。

If you have filters for 401, 403 and 500 that will tidy up your controller slightly. 如果您有用于401、403和500的过滤器,则可以稍微整理一下控制器。 You will still need to add attributes for certain methods that can't be dealt with by Reflection. 您仍将需要为反射无法处理的某些方法添加属性。 With this method I find I only need to add one or 2 attributes, typically [ProcudesResponseType((int)HttpStatusCode.BadRequest)] and [ProcudesResponseType((int)HttpStatusCode.NotFound)] . 通过这种方法,我发现只需要添加一个或2个属性,通常是[ProcudesResponseType((int)HttpStatusCode.BadRequest)] [ProcudesResponseType((int)HttpStatusCode.NotFound)] [ProcudesResponseType((int)HttpStatusCode.BadRequest)][ProcudesResponseType((int)HttpStatusCode.NotFound)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM