Swaggerui interface document in ABP Add upload file parameter type

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.