How to integrate Swagger in .Net Core API

Riti Tiwari
.Net Core API
Published in
2 min readApr 23, 2019

Swagger is an open-source software framework that helps developers design, build, document, and consume RESTful Web API. Swagger is popular for its Swagger UI that allows developers to test their Web APIs interactively.

However, Swagger toolset supports automated documentation, code generation, and automated testing including test cases.

Steps to integrate Swagger :

Step 1. Create.Net Core WebAPI project using Visual Studio 2017

Step 2. Install Swashbuckle.AspNetCore package from NuGet

Step 3. Add below methods — Call the bootstrapper in “Startup” class

Step 4. Enable generating XML documentation

To enable XML documentation, right click on your Web API project ”Properties” then choose “Build” tab, after you choose it, scroll down to the “Output” section and check “XML documentation file” checkbox and set the file path to: “bin\[YourProjectName].XML” as the image below. This will add an XML file to the bin folder which contains all the XML comments you added as an annotation to the controllers or data models.

Step 5. Add Route attribute, if you already haven’t added as below:

So in the above code, you will see I have added Comments for Swagger using “///” slashes.

Step 6. Simply run your project and navigate to https://localhost:44378/swagger — You will have to set up or change your localhost URL

Above are the APIs available in the code base

Conclusion

Swagger is a great tool to test your RESTFul APIs. Once integrated, it creates a simple UI to list down all your APIs actions and provide details about the request and response object for each API action. Integration with ASP.NET Core 2.0 is also straightforward and requires minimal efforts.

Thank you for reading. Keep visiting and sharing this in your network. Please put your thoughts and feedback in the comments section.

Feel free to contact me in case of any queries, Good luck!!!

#.NetCoreStarters #.NetCore, #API, #Swagger #JWT #APIDocumentation

--

--