Tool Spotlight

Swagger and Swashbuckle with ASP.NET Core API

This post is going to walk through adding Swagger to an existing ASP.NET Core API application using Swashbuckle. The starting point for the code can be found here.

What is Swagger?

Swagger is a specification on documentation an API. As I am sure we all know API documentation tends to get out of date fast and a lot of times is a low priority.  Swagger aims to help solve that problem using a format that is both human and machine readable which can be maintained in either JSON or YAML and can be auto generated using a tool like Swashbuckle. Check out this post by the Swagger team for the full introduction.

What is Swashbuckle?

Swashbuckle provides auto generation of Swagger 2.0, swagger-ui integration, etc. The project takes all the pain out of getting going with Swagger as well as providing tools and hooks for using and customizing Swagger related items. The full description can be found here.

Adding Swashbuckle to the project

There are lots of ways to get a new package into an ASP.NET Core application and the following covers the NuGet UI, Package Manager Console and Project.json. Pick one of them to use.

NuGet UI

Right click on the project Swashbuckle is going to be added to, Contacts in the case of the sample code, and select Manage NuGet Packages.

swashbuckleprojectmenu

Select the Browse tab, check the Include prerelease checkbox and search for Swashbuckle. Prerelease is need to get the version that works with ASP.NET Core.

swashbucklenuget

Finally click the Install button and work though any confirmation dialog screens that might show.

Package manager console

From the package manager console run Install-Package Swashbuckle -Pre.

Project.json

Open porject.json and in the dependencies section add “Swashbuckle”: “6.0.0-beta902”.

Add and configure Swashbuckle

In the ConfigureServices function of the Startup class add the following. I added it as the end, but placement shouldn’t matter.

services.AddSwaggerGen();

Next in the Configure function after app.UseMvc add the following.

app.UseSwagger();
app.UseSwaggerUi();

The first line enable serving of the Swagger JSON endpoint and the second enables the swagger-ui.

Test it out

Running the application will now provide two new routes one or each of the items added to the Configure function above.

The first is http://localhost:13322/swagger/v1/swagger.json (your base URL may differ if not using the sample procject) and it exposes the Swagger compliant JSON.

The second URL is http://localhost:13322/swagger/ui and it provides a very readable view of the documented API along with examples and options to try the API out. The following as an example of what the current version outputs.

swaggeruiexample

Wrapping up

Swashbuckle make it easy to add Swagger to a project. I feel that it also provides a huge value for anyone trying to consume an API. It is of course not a magic bullet and communication with your API consumers about API changes will still be critical.

Microsoft’s docs has a great walk through which can be found here. It does more in-depth on customizing your setup and as far as modifying the look of the UI.

The code for this post in it’s finished state can be found here.

Swagger and Swashbuckle with ASP.NET Core API Read More »

Tool Spotlight – Add New File and Open Command Line Visual Studio Extentions

Tool Spotlight is a new type of post that I am trying out to bring attention to tools that I find useful and worth sharing. These entries will not be on any sort of schedule and will get posted anytime I come across a tool worth mentioning.

This post is going to cover two Visual Studio extensions that I find very helpful especially in the context of writing ASP.NET Core applications. Both of today’s extensions were written by Mads Kristensen.

 Add New File

The Add New File extension provides a simple way of adding new files without having to go through Visual Studio’s add new item dialog. Visual Studio’s dialog is great when I need it, but a lot of times I am adding a html or JavaScript file and just want a blank file and this extension provides a mechanism to do just that.

Lets look at the difference in the two dialogs. Here is Visual Studio’s dialog:

defaultAddNewFile

And here is what the add new file’s dialog looks like:

addNewFile

As you can see the extension’s dialog is much simpler. After the extension is install simply hit Shift+F2 to show the above dialog, enter a file name and click Add file to create a file in the current directory. Directories will also be crated as needed.

Open Command Line

The Open Command Line extension provides a way to open a console from within Visual Studio. The default console application is configurable with options from cmd and PowerShell to cmder and custom user defined applications. Opening the default console to the current directory of the selected item in Visual Studio’s solution explorer can be triggered using the Alt+Space shortcut. The extension also adds an Open Command Line option to the right click menu in solution explorer.

openCommandLineRightClick

Thoughts and Suggestions

Leave any thoughts on if this sort of post is helpful, your favorite tools or suggestions for future topics in the comments.

Tool Spotlight – Add New File and Open Command Line Visual Studio Extentions Read More »