From the course: Building Angular and ASP.NET Web API Apps
Create an empty Web API Project
From the course: Building Angular and ASP.NET Web API Apps
Create an empty Web API Project
- [Instructor] Welcome to this part and this section. In this section we are going to set up the ASP.NET Web API project. We are going to create all the necessary API endpoints to create transactions to read them, update them, and delete them. We are going to set up the entity framework to work with an SQL database, and by the end, we are going to test everything that we did create. Before we start creating any of the API endpoints, we are going to create an empty ASP.NET Web API project. For that, let's go to Visual Studio. Once you start Visual Studio, you can either create a new project using this button or continue without code. Once you have this view, you go to File, New and then Project. The first step is going to be to choose the project template. We are going to create an ASP.NET Web API so let us search in here for asp.net web api. You're going to get multiple options. We're going to use the first one, which is C#, and then ASP.NET Core Web API. Then Next. Here you need to define the Project name and the Solution name. This is going to be the API for the ExpensesApp, so I'm going to name this project the Expenses.API. As you can see, the Solution name is also updated, but you can always change the Solution name without having to change the Project name. And that is because inside a single solution you can have multiple projects. For the project solution, I'm going to just select the ExpensesApp folder that I've created in my C drive. You can use any folder that you want. Then Next. In the next step you need to choose the framework version. Because I have installed multiple versions of .NETs or different .NET SDK versions, in here I get all the options. I'm going to use the latest one, which is the .NET 9.0, but the steps are going to be the same if you're using any version of .NET, starting from the version 5.0. I'ma just select the latest version in here. The authentication type is going to be None because we are going to set up the token based authentication in an upcoming chapter. Configure for HTTPS is going to be checked because we want our app to run in HTTPS. We will not enable container support. We are going to leave the Enable OpenAPI support checked, and then the Use controllers checked. Click Create. Once the project is created, you are going to get the overview in here where you can check some documentation, but I'm going to close it. And then just go here to the right to the Solution Explorer. Now if you don't see the option in here, you just go to View and then Solution Explorer. These are the basic or the default files that come with an ASP.NET Web API project. And here we have the Connected Services, which means that you can just connect your app to a third party service. You have Dependencies and in the dependency section we are going to see all the NuGet packages that we're going to install. We have Properties where you can define different launch settings. If I open this file, we are going to see that we have, by default, two profiles. The first one is the HTTP, the other one is the HTTPS. And what this means is that if you run the app using this profile, then the application URL is going to be the localhost and then 7145. You see these options here at the dropdown at the top. We have HTTP, HTTPS, and two other default settings. And next we have the Controllers. This is a really important folder because inside here we are going to put our own controllers. In the appsettings.json file we are going to store the database connection string. Then we have the Program.cs, which is a really important file in all the ASP.NET projects and that's because the Program.cs is the entry point of the .NET apps. And here at the end we have the WeatherForecast, which is just a dummy or just an example class in C#. The other file you see in here is just a file that can be used for testing. So I'm just going to ignore this one. And then I'm just going to run the app. Because I'm using the HTTPS profile, the app is going to run in this link. As you can see now, the app is running in localhost:7145. Let me just copy this link, and then I'll just go to the browser. But in here you can see that we don't get any swaggers or anything, which you normally get in lower versions of ASP.NET. And that is because in the .NET 9.0, they have removed this Swagger dependency by default. So if you want to have this Swagger page, you need to configure it separately. So if I just type right now in here, swagger, and then index.html, I don't get anything. So let us go to Visual Studio. Let us configure Swagger because we want to use Swagger for testing. For that, the first thing to do is that to just install the Swagger package. So just go to Dependencies, then go to Manage NuGet Packages, then go to Browse and search for Swashbuckle. So Swashbuckle.AspNetCore. Select the first option and then Install. Now that the package is installed, then I'll just go to Solution Explorer, then Program.cs. And here you can see that by default we have the OpenApi support, but the OpenApi, so basically the AddOpenApi, and the MapOpenApi down here, do generate an OpenAPI JSON document, but this doesn't include Swagger UI by default. To get this Swagger UI, we can just remove this line, and then you just type builder.services.AddEndpointsApiExplorer. And then the second line is going to be builder.Services.AddSwaggerGen. The first line is required for the endpoint metadata and the second one adds Swagger document generation. Then down here, let us remove the MapOpenApi, and then just type app.UseSwagger, and then app.UseSwaggerUI. The first line in here serves the OpenAPI JSON document, and the second one serves the Swagger UI. Now one more thing that we need to change. As you could see when we run the app, it just opened the terminal, but it didn't start or it didn't open the browser. Let us change that as well. So, go to launchSettings.json and then in here, change the launch browser from false to true. And then we are going to add one more property, which is going to be that the launchUrl is going to be the "swagger". So what this means is that whenever we run the app, the app is going to run in https://localhost:7145/swagger, which is going to open the Swagger UI. We're going to use Swagger throughout this course to test the API endpoints. Now in here because I added the launch browser through to the profile HTTPS, let us do the same for the HTTP. So true, and then let us also set the second value to be "swagger". Now let us save all the changes and then start this project. This is going to open the browser and then down here you can see it. We have a single controller named WeatherForecast, which has a single API endpoint of type HTTP GET. And down here we also have the models, or the C# classes, and the default one was WeatherForecast. Now this is all for this part. On the next one, we are going to define the application models, so see you on the next one.
Contents
-
-
-
-
Create an empty Web API Project9m 4s
-
(Locked)
Define application models5m 47s
-
(Locked)
Configure entity framework for database access13m 55s
-
(Locked)
Build the Create API endpoint10m 41s
-
(Locked)
Create the read-all API endpoint3m 4s
-
(Locked)
Create the read by ID API endpoint6m 55s
-
(Locked)
Create the update API endpoint7m 4s
-
(Locked)
Create the delete API endpoint3m 10s
-
(Locked)
Cleanup Code16m 24s
-
-
-
-
-