How to add OpenAPI and Swagger to ASP.NET Core Minimal APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there friends jeff fritz here again and we've been talking about minimal apis and slowly growing them and moving things around to make them easier to manage well in this video we're going to add open api support to our minimal apis with just a few lines of code and an additional nuget package let's take a look at how we can take our contacts api that we've defined here and start adding swashbuckle the package to enable swagger and open api interactions all right let's do it first thing i need to do is introduce the package swashbuckle we do that with a very simple command dotnet add package right here at the command line you can use the nuget package explorer if you're in visual studio 2022 or visual studio 2019 and we're going to add swashbuckle dot asp.net core right there okay i'll press enter and it's going to go locate that package for us and install all the capabilities into this project so i will clear everything out there all right so let's introduce those swagger capabilities into our application so first thing that i'll do is i'll add some definition here for the builder to get access to the endpoints api explorer this is how the swashbuckle package is going to navigate around and find all the api endpoints inside of this asp.net core web server next i'm also going to add the ability for swagger generation generate that contract information that swagger tools can interact with and open api contracts can produce and interact with as well let's specify here we're going to create and do some configuration here so i will say c dot swagger doc and the name i'll give this is v1 and i'll give it some open api info here so we'll specify a title uh fritz's contacts api and we'll also give this some version information as well so version equals v1 again and i can put a break right there no i need one there and i also need one there okay next i need to configure the http pipeline further down here where we're mapping the interactions i need to configure it so that it knows to listen for swagger requests and also to generate a user interface that folks can interact with so down here i'll add right here app dot use swagger and i'll also configure that swagger ui now the swagger ui has some options that we want to pass into it to configure and make it a little bit more interesting so i'll specify a swagger endpoint and i want to specify the location of that endpoint so let's listen to swagger slash v1 slash swagger.json and i'll give it a name well it's it's the v1 endpoint so i'll just put that on there done that's all it took was those four lines of code with a little bit of extra configuration information there's a bunch more configuration information that you can add into this but we're just scratching the surface here in this minimal api series so you can see what you can work with there's links in the description below so you can go to the official microsoft documentation and dig in further on how to use swashbuckle with open api and minimal apis in.net 6. our web server is running let's go take a look at that contacts page and there it is there's fritz and crystal okay and we can go take a look at the api for this and explore around it by going to slash swagger and there's fritz's contacts api it's really large let me zoom back out a little bit and you can see it's version one there and there's a couple of features that this provides you can access gets on the route you can go to get contacts and you can get contacts slash id and if we open that one up we can specify an id it knows that we can do that and it's going to return success so let's try this out i can specify here i'll request id2 and i can execute that and if i scroll down it shows me exactly how it's executed the location that it navigated to and there's the response it returned crystal from dallas there's a lot more you can do with open api to add into your minimal api projects to make it a lot more descriptive about what's going on inside of the application and it's going to build and generate these contracts for you without you having to write special code around the actual contracts the actual apis that you've got living out there inside that that contact extensions file that we wrote previously i didn't have to modify that this was all generated for me based on the contents of my apis in the way that asp.net core behaves thank you so much for watching hey if you like this series of videos make sure you click the like button below click the subscribe button and make sure you click that bell so you can be reminded of when more of my videos are going to be published out to youtube thanks so much for watching and make sure you check out the live series over on twitch i'll see you there
Info
Channel: Fritz's Tech Tips and Chatter
Views: 8,362
Rating: undefined out of 5
Keywords: asp.net core, aspnetcore, c#, dotnet, dotnet6 .net6, minimal api, minimalapi, openapi, swagger, swashbuckle, openapi .net, swagger openapi .net core, openapi generator .net core, c# openapi client generator
Id: _GHfvsl1274
Channel Id: undefined
Length: 6min 16sec (376 seconds)
Published: Wed Jan 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.