OpenAPI definitions can be created, updated, and shared with clients using this tool. This tool is primarily used by developers to document APIs. We’ll look at using swagger with .NET Core 6 for working with multipart/form-data.
Step By Step Guide:
Open Visual Studio and select Create a new project to create Web API applications for .NET core 6.
Choose .NET 6 as a framework when you click on the ASP.NET Core Web API template.
Add the following file to the model folder.
Photo.cs
public class Photo
{
public IFormFile formFile { get; set; }
}
Add the following controller to the controller folder by right-clicking on it
PhotosController.cs
public class PhotosController : ControllerBase
{
[HttpPost]
public async Task<ActionResult> PostAsync([FromForm] Photo photo)
{
var fileName = photo.formFile.FileName;
return Ok();
}
}
Incorporating multipart/formdata into Swagger
ASP.NET Core Web API includes an already configured Swagger API in Program.cs
Start the application
Upon loading the Swagger page in the browser, then select the photos post resource, and click on Try it Out.
Upload the image by clicking on Choose file. When you click on execute, an image information will be sent to the API.
The action returned Image details as expected.
In Swagger, we should use [FromForm] for multipart/form-data requests.
Conclusion:
So in this post, I have tried to cover how you can send data in FormData using swagger in ASP.NET Core 6. If you have any queries or confusions do not hesitate to ask in the comments below.