Creating and Configuring a New Web API Project
ASP.NET Core Web API comes with some powerful configuration options right out of the box. Let's see what those are in this 10-minute training video. Now for most of my training, I work to give you an in-depth perspective on a technology, but sometimes you just need to get the quick answer to the question, "How do I use this?" That's why I created the 10-minute training series.
So let's dive right into Visual Studio and we're going to start this time right from scratch. So file, new project and we're going to choose the API project type. So ASP.NET Core Web API, we's going to refer to as API from now on, but that's the full official name is ASP.NET Core Web API and we're going to call this our API demo and API demo app. We hit next. We're going use .NET 6, no authentication. We're going to leave that off but you can turn it on and add authentication to a Microsoft Identity Platform or to Windows. We're going to leave HTTPS on. We're not going to enable Docker, but that's another thing right out of the box you can add.
So also, we're going to use controllers. We could uncheck this and use a minimal API, but I want to have the full controllers for this demo. And we are going to enable Open API support, which we're going to see in this demo because the fact that it's one of those great features. Now leave the top-level statements on, which means uncheck this box. Don't leave that box checked. That will revert back to not using top-level statements. Create. And this is going to give us a standard out-of-the-box basic API.
Alright, so we have one controller. We're actually going to get rid of that controller and the weatherforecast.cs. Let's just do that now. We'll clean this project up to start from nothing. So there is our project and let's add one controller to it just to have a sample. So one controller, and we're going to choose under API the API controller with read write actions, not the one using Entity Framework. Hit add and we're going to call this the Users controller. And there we have our start.
Feature 1: Automatic Documentation with OpenAPI (Swagger)
Alright, so that's just a start of our project. So this is out of the box the API project, except for the fact we removed the sample code and added a little bit of sample code of our own. And the reason why is because this gives us more of the CRUD actions, the full read and write actions in a demo format.
Okay, so with this right out of the box, the first thing that you get with API is OpenAPI. Now, what is OpenAPI? It's also known as Swagger. So if you're using no Swagger, they've changed it to be OpenAPI. This is a standard that's not a Microsoft thing. It's actually a web standard thing. And so they moved to calling it OpenAPI and it's part of a standard. Well, Microsoft has implemented it by default. So we have AddSwaggerGen and then down here, UseSwagger and UseSwaggerUI. So whenever you see Swagger, just know it's OpenAPI.
Let's just right out of the box see how this works in our application. Whoops, let's move it off to the side for a minute and hit run. Once it runs, we'll bring it back. There we go. So there is our API, but notice it's actually running on the Swagger page. And the reason why is because this OpenAPI is a tool that allows us to very quickly and easily document our API. Remember that I created a new user controller? Well, that's already documented for me, and I can try it out right in the browser and see values being returned. So right out of the box, we have Swagger, and that is something you should have for your application because Swagger will not only allow you to very easily give a documentation set to your customers saying, "Hey, here's how my API works," but also notice this link right here. This link is a JSON file with all that same information. Why is that a benefit to us? You can actually create a client based upon that automatically. You can autogenerate a client to connect to this API. So that's one of the reasons why this is so great. We can also have different versions of our API and this will handle those versions. Now that is something that does not come out of the box, but it is something you should do in your API from day one. You should version your API. Without that, you run the risk of causing problems down the road. But OpenAPI is the first thing.
Feature 2: Built-in Dependency Injection
The next thing that's built right in, and we'll see that right here in this section, notice builder.services. We have dependency injection. That's what this builder.services is. We have dependency injection set up for us by default. It's just done. If we have dependencies, we add them right here and we can use them in dependency injection. So it's done for us. That's number two.
Feature 3: Advanced Configuration with App Settings & User Secrets
Number three: appsettings.json. People see this and think that it's really basic, but it is so far from basic. This is not the same as web.config from .NET Framework. You see we have appsettings.json, that's the basics. But then we have appsettings.development.json which will override appsettings.json. But wait, there's more. Because this all gets put into source control. But if you right-click on the project and say manage user secrets, this is a secrets file that will override both appsettings.development.json and appsettings.json with information from your local machine that does not get put into source control. That way you can have local testing information that does not get put into Git. It's a great tool for having a local development setup without disturbing everyone else's version of that. So secrets.json as a part of appsettings.json is a really key feature you should be using.
Feature 4: Integrated Logging with ILogger
Also built into this, it's not necessarily configured, but you can configure and use it through just asking for it. So you can say constructor and you can say ILogger of type UserController, whatever the class name is that you're in, and call it logger. And then you can use this logger right here throughout your application to log to whatever logging provider is configured. By default, you have one set up right out of the box, but changing it is just a matter of configuring your service and that's it. You configure a different service for your logger and away you go. You should definitely be using loggers.
Also, this comes back to OpenAPI. We can document. Let's come down here and say one, two, three slashes. This was called an XML code comment. This comment can be used to document this action. But even better, this can then get put into OpenAPI. That's the fifth thing.
And finally, we have data validation. So if we create a new folder called models and in here we create a user model, and in here we say prop string FirstName, well we can say here this is required. And using that using statement, this will now make sure that this is required. We can also say that it has to have a min length of one character or 10 characters, whatever we want. And that validation we apply to our models, we can then use it. This is how we can verify that our data is correct by adding this data validation. So that is the sixth thing that comes out of the box with API that's important.
Beyond the Basics: More Essential API Features
But there are more things like authentication, versioning, health checks, caching, rate limiting. These are things that you should be setting up with every API.
Now I do want to point out, let's go back here and say iamtimcorey.com. If we go here and look at the Web API course, you don't have to buy this, you don't have to buy this, but down here we have the curriculum. And if you expand this, this will tell you all the things that I cover in my 18-hour course, and you can look up each of these on your own and learn how to use them because I think these are all important to building a world-class API.
Alright, that's my six things you need that come out of the box in API and again, five more that are important as well.