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