@ VisualBean के जवाब
विस्तार एक नियंत्रक के एपीआई विधि पर आप की तरह एक गुण सेट करने के लिए नीचे दिए गए कोड का उपयोग कर सकता:
[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)]
public HttpResponseMessage GetAuthorityForm(string id)
{
....
नोट: 'विशेष = सच' अन्य सभी प्रकार की सामग्री को हटा देगा, अन्यथा नई विशेषता का उपयोग करके स्वैगर यूआई ड्रॉप डाउन में एक नया प्रतिक्रिया सामग्री प्रकार जोड़ देगा। यह आपके कंट्रोलर या एपीआई को सिर्फ दस्तावेज को संशोधित नहीं करेगा।
SwaggerConfig.cs
GlobalConfiguration.Configuration
.EnableSwagger(c =>
// Set filter to apply Custom Content Types to operations
//
c.OperationFilter<ResponseContentTypeOperationFilter>();
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);
}
}
}
शशबकल 5 को आपके लिए यह ध्यान रखना चाहिए यदि आप वेब एपीआई कॉन्फ़िगरेशन के दौरान MediaTypeFormatter पंजीकृत करते हैं। –
धन्यवाद। यह चालाक लगता है, लेकिन मैं इसे प्रति कार्य/मार्ग चाहता हूं। –
मुझे लगता है कि आपको इसे अपने वेबकॉन्फिग में एक फॉर्मेटर के रूप में जोड़ने की आवश्यकता है - यह प्रति क्रिया के आधार पर वैश्विक नहीं है। आप क्या कर सकते थे अपने स्वयं के ऑपरेशन बना सकते थे फ़िल्टर करें और इसे केवल उन परिचालनों पर लागू करें जो नए प्रारूप – VisualBean