When uploading a file using Swashbuckle, you want to see the upload control in the interface document, but in C # there is no Frombodyattribute this feature, so you need to modify the Swagger property of the parameter at run time. First look at the final effect: the implementation is described below. Implement the principle, through the filter provided by swagger, find the parameters with the Swaggerfileupload attribute in the action, and then add a custom parameter to swagger Operaion.parameters, that is, the file type parameter. (1)
define Swaggerfileuploadattribute.
[AttributeUsage (attributetargets.parameter)] Public class swaggerfileuploadattribute:attribute{publicboolgetprivateSet Public Swaggerfileuploadattribute (booltrue) {This . Required = Required;}}
View Code
(2)
add filter.
/// <summary>///Swagger File Upload parameter filter/// </summary> Public classswaggerfileuploadfilter:ioperationfilter{ Public voidApply (Operation operation, Schemaregistry Schemaregistry, apidescription apidescription) {varParameters =apiDescription.ActionDescriptor.GetParameters ();foreach(Httpparameterdescriptor Parameterdescinchparameters) {varFileuploadattr = parameterdesc.getcustomattributes<swaggerfileuploadattribute>(). FirstOrDefault ();if(Fileuploadattr! =NULL) {OPERATION.CONSUMES.ADD ("Multipart/form-data"); OPERATION.PARAMETERS.ADD (NewParameter{name= Parameterdesc.parametername +"_file", @in="FormData", Description="file to upload", Required=Fileuploadattr.required,type="file"});}}}}
View Code
(3)
set filter for swagger.
(4)
parameters in action set properties, test. public void Testswaggeruploadfile ([swaggerfileupload] file) {} above four can achieve the beginning of the article effect.
Swaggerui interface document in ABP Add upload file parameter type