Wednesday, October 9, 2019

Tip: How to add form data as a parameter(s) into Swagger UI

Recently I come across the need to pass form data from an Angular form to a web API written in ASP.NET MVC. The web api does not accept any input parameter specifically via the function signature. However i need to access some form values via the HttpContext.Current.Request.Form. Unfortunately the form does not provide any text box to enter the necessary payload.

After doing some research I found out how to make Swagger to show an extra text box just for that particular web api. First of all I need to define a function attribute that i can use for the web api, and also to tell Swagger that the function actually consist of extra parameters that need to shown in the UI. The code looks like the following:

The next piece of the code shows how we can register the attribute filter to the Swagger. The code tells Swagger to go through every Controller class in the project and check for the specified filter and in turn inform the Swagger UI about any extra parameters to create.
As you can see during application startup, the Swagger will trigger this code via the following configuration in SwaggerConfig.cs file.
The following code shows how the attributed is applied:








The picture below shows the end result in Swagger UI:





No comments: