Swashbuckle Swagger - How to annotate content types?

前端 未结 4 826
南方客
南方客 2020-12-16 12:02

How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support?

Specifically, I want the documentation

4条回答
  •  爱一瞬间的悲伤
    2020-12-16 13:01

    Following on OzBob's answer. Since Swashbuckle 4.0.x, you may need to update the operation filter code slightly:

    ResponseContentTypeOperationFilter.cs

    using Swashbuckle.AspNetCore.Swagger;
    using Swashbuckle.AspNetCore.SwaggerGen;
    using System.Linq;
    
    public class ResponseContentTypeOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (!context.ApiDescription.TryGetMethodInfo(out var methodInfo))
            {
                return;
            }
            var requestAttributes = methodInfo.GetCustomAttributes(true).OfType().FirstOrDefault();
    
            if (requestAttributes != null)
            {
                if (requestAttributes.Exclusive)
                    operation.Produces.Clear();
    
                operation.Produces.Add(requestAttributes.ResponseType);
            }
        }
    }
    

提交回复
热议问题