Adding Swagger help page to Web API project

I'm a software consultant who loves to build projects and share my learnings on this blog.
Search for a command to run...

I'm a software consultant who loves to build projects and share my learnings on this blog.
No comments yet. Be the first to comment.
Azure Functions is a powerful serverless compute platform, and Azure App Service handles a lot of the underlying configuration for you out of the box. However, there are scenarios where you may want t

If you’ve been exploring Azure AI learning paths or watching older demos, you’ve probably run into something confusing: The documentation and training links often reference Foundry (Classic) But the portal you’re using today might show Foundry (New...

In today's digital age, speed is paramount for web and mobile applications. Users expect instantaneous responses, and any delay can lead to a poor user experience and loss of engagement. Therefore, as developers, it's crucial to build web APIs that a...

Loops are fundamental constructs in any programming language. But have you ever wondered if there are any performance differences between them? For small collections, the difference might be negligible, but what about handling a million or 10 million...

When using an Azure Function app, it is necessary to connect it to a storage account, often referred to as Host storage. This storage is utilized by the Functions runtime, as well as by various triggers and bindings to coordinate between multiple run...

Documentation is a key integral of any project. And when it comes to a Web API project, it's a must, mainly for the client side to know the APIs to call on the server and to handle the expected response objects. Manual documentation of the APIs, their request body, their responses is definitely out of the question as the APIs keep changing through the life cycle of the project. This is where Swagger comes into the picture.
The evolution of your API’s functionality is inevitable, but the headache of maintaining API docs doesn’t have to be. Swagger tools takes the hard work out of generating and maintaining your API docs, ensuring your documentation stays up-to-date as your API evolves.
To add swagger to your web API project, you need to install the Swashbuckle NuGet to the project.
Right click on your API project and choose "Manage NuGet Packages". In the Package manager window, search for "Swagger" or "Swashbuckle" and install it.

And that's it, swagger would be enabled on your project. Run the project and navigate to "http://YOUR_PROJECT_URL/swagger" for e.g. "http://localhost:1357/swagger" and you would see the swagger API documentation for your project.

Well, though just installing a NuGet got the job done, it's good if you know how things are wired up and what changes are recommended to do.
If you look inside your App_Start folder, you would notice a new auto-generated file called SwaggerConfig.cs and this is where the configuration for Swagger exists.

At the minimum, you will need the following configuration to enable swagger. (This is the one that's already there inside the SwaggerConfig.cs)
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "API Title");
})
.EnableSwaggerUi(c =>{});
And if you notice the line above the namespace, you would find this line of code that does the wiring up ( registering it )
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
We generally don't want Config registrations lying around here and there and always prefer to keep it in a single place - the Global.asax file. Go ahead and comment/delete that line from your SwaggerConfig and add SwaggerConfig.Register(); to your Global.asax file. So your Global.asax file should look something like this at the bare minimum :

So now the registration happens from the Application_Start() method, and all our config registrations are in a single file.
The next important question, why swagger? What benefits does it provide?
The main thing is that each public action method in your controllers can be tested from the Swagger UI. Click a method name to expand the section, add the necessary parameters, if any and click Try it out!. You can see the responses as well as the expected model.

Similar one goes for a POST request as well. You can see the expected model for the request and test it out with a sample model from the Swager UI itself.

No need for POSTMAN or other tools for testing the API. Pretty cool right!! Don't forget to try out Swagger in your next project.