. Net core tips-uploading files using swagger

Source: Internet
Author: User

Objective

With the popularization of the development pattern of the back-end separation, the backend staff writes the service-side API interface more. Call interface implementation of File upload is a common feature, but also need a choice of file upload interface, you can write the front-end interface upload, you can use postman, curl to simulate the upload request. The above method is somewhat troublesome. Swagger as the API documentation and debugging tools, if it can provide a file upload interface (not provided by default), it will be more convenient for file upload prompt, this article will explain how to use swagger to upload files.

Steps

1. Installing swagger

Install-package Swashbuckle.aspnetcore

2. Configuring Swagger Middleware

Add in Startup.configureservices:

Services. Addswaggergen (c +=    {c.swaggerdoc ("v1"new"My API  "v1 " } );

Add in Startup.configure:

== {    C.swaggerendpoint ("/swagger/v1/swagger.json""My API V1");});

3. Writing the API

// POST api/values [HttpPost]  Public void Post (iformfile file) {    //todo:save file ... }//  PUT api/values/5[Httpput ("{ID}")]  Public void Put (int  ID, iformfile file) {    //todo:save file ...}

4. Writing Swaggerfileuploadfilter

 Public classswaggerfileuploadfilter:ioperationfilter{ Public voidApply (Operation operation, Operationfiltercontext context) {if(!context. ApiDescription.HttpMethod.Equals ("POST", stringcomparison.ordinalignorecase) &&!context. ApiDescription.HttpMethod.Equals ("PUT", StringComparison.OrdinalIgnoreCase)) {            return; }        varFileparameters = context. ApiDescription.ActionDescriptor.Parameters.Where (n = n.parametertype = =typeof(Iformfile)).        ToList (); if(Fileparameters.count <0)        {            return; } operation. Consumes.add ("Multipart/form-data"); foreach(varFileparameterinchfileparameters) {            varparameter = operation. Parameters.single (n = n.name = =fileparameter.name); Operation.            Parameters.remove (parameter); Operation. Parameters.Add (NewNonbodyparameter {Name=parameter. Name, in="FormData", Description=parameter. Description, Required=parameter. Required, Type="file"            }); }    }}

5. Register Swaggerfileuploadfilter

C.operationfilter<swaggerfileuploadfilter> ();

6. View Results

Post method:

Put method:

Principle Analysis

The key to uploading files using swagger is Swaggerfileuploadfilter, which inherits from the ioperationfilter of swashbuckle, i.e. it only acts on swagger and does not affect other modules.

Let's take a look at the logic of the Apply method inside Swaggerfileuploadfilter:

1. The method of judging whether the request is post or put, if it is other request method, it is basically impossible to file upload operation.

2. Find a parameter of type Iformfile in the method.

3. Set consumes to "Multipart/form-data".

4. Replace iformfile in the type parameter description in the Formdata, type is file.

Source Address

Https://github.com/ErikXu/.NetCoreTips/tree/master/SwaggerFileUpload

Resources

http://www.talkingdotnet.com/how-to-upload-file-via-swagger-in-asp-net-core-web-api/

  

. Net Core tips-uploading files using swagger

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.