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
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);
}
}
}