@VisualBean answer extension
In the Controller API method, you can use the code below to set the type attribute:
[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)] public HttpResponseMessage GetAuthorityForm(string id) { ....
Note: "Exclusive = true" will delete all other content types, otherwise using the new attribute will add a new response content type in the Swagger user interface. It will not change your controller or API in the documentation only.
Swaggerconfig.cs
GlobalConfiguration.Configuration .EnableSwagger(c =>
SwaggerReponseContentTypeAttribute.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); } } }
Ozbob
source share