Creating ASP.NET Core Web API Project in Visual Studio 2022

Creating ASP.NET Core Web API project in Visual Studio 2022

In this article, I am going to discuss How to Create, Build, Run, and Test the ASP.NET Core Web API Project in Visual Studio 2022 using .NET 6. Please read our previous article, where we discussed How to Create, Build, Run, and Test the ASP.NET Core Web API project using .NET Core CLI and Visual Studio Code. Now we will see the step-by-step process to create an ASP.NET Core API project using Visual Studio 2022 with ASP.NET Core 6.0:

Creating ASP.NET Core Web API Project Using Visual Studio 2022 using .NET 6

Now, we are going to see the step-by-step process to create the ASP.NET Core Web API project using Visual Studio 2022 with ASP.NET Core 6.0. So, open Visual Studio 2022 and then click on the Create a new project option as shown in the below image.

Creating ASP.NET Core Web API Project Using Visual Studio 2022

Once you click on the Create a new project option, the following Create a new project window will open. Here, you need to select the “ASP.NET Core Web API” template which uses C# as the programming language, and then click on the Next button as shown in the below image.

Creating ASP.NET Core Web API Project Using Visual Studio 2022

Once you click on the Next button, then the Configure your new Project window will open. Here, you need to specify the Project name (MyFirstWebAPIProject) and the location where you want to create the project. And finally, you need to click on the Next button as shown in the below image.

How to Create, Build, Run, and Test the ASP.NET Core Web API Project in Visual Studio 2022

Once you click on the Next button, it will open the Additional Information window. Here, you need to select the Target .NET Core version. The authentication Types. Whether you want to configure HTTPS and enable Docker, etc. We are going to use .NET 6 which provides Long Term Support (LTS), so, select .NET 6. We are not going to use any kind of authentication at this moment, so, select authentication type as None. Then apart from Enable Docker, checked the rest of all the checkboxes such as Configure for HTTPS, Use Controllers, Enable OpenAPI Support, Do not use top-level statements, and then click on the Create button as shown in the below image.

How to Create, Build, Run, and Test the ASP.NET Core Web API Project in Visual Studio 2022

The meaning of the above checkboxes is as follows:

  1. Configure for HTTPs: If checked, then you can access the endpoints using both HTTP and HTTPS protocols.
  2. Enable Docker: If checked, then it will enable Docker.
  3. Use Controllers: If checked, then it will use controllers instead of minimal APIs.
  4. Enable OpenAPI Support: If checked, then automatically you will get swagger support for your application.
  5. Do not use top-level statements: If checked, then it will create an explicit Program class and Main method instead of top-level statements.

Once you click on the Create button, it will create the ASP.NET Core Web API project with the following file and folder structure. You can see the Solution Explorer window on the right side, displaying your project folders and files. In our next article, we will discuss all these files and folders in detail.

How to Create, Build, Run, and Test the ASP.NET Core Web API Project in Visual Studio 2022

That’s it! You have created an ASP.NET Core API project using Visual Studio 2022 with ASP.NET Core 6.0. You can continue building your API by adding more controllers, models, and services as required.

Points to Remember:
  1. You can modify the default controller or create new controllers as per your requirements. Right-click on the “Controllers” folder and select “Add” > “Controller” to add a new controller.
  2. Choose the type of controller you want to add (e.g., “API Controller with actions, using Entity Framework”), and click “Add.”
  3. Visual Studio will scaffold the controller for you, including default CRUD actions. You can customize the generated code to fit your needs.
  4. To test your API, you can run it using the built-in IIS Express or a local development server. Press F5 or use the “Start Debugging” button to run your application.
  5. Once the API is running, you can send requests to the specified endpoints. By default, the URLs will be based on the controller and action names.
How to Build the ASP.NET Core Web API Project in Visual Studio 2022?

You can build the ASP.NET Core Web API Project in many ways. So, let us discuss them. From the menus, select Build => Build Solution as shown in the below image.

How to Build the ASP.NET Core Web API Project in Visual Studio 2022?

Once you select Build => Build Solution, then it will build all the projects that are there in the solution. Once the build is successful, you will get the below message in the output.

How to Build the ASP.NET Core Web API Project in Visual Studio 2022?

You can also build your project by right-clicking on your project and then selecting the build option from the context menus as shown in the below images.

How to Build the ASP.NET Core Web API Project in Visual Studio 2022?

You can also right-click on your solution and select the Build Solution option to build all your projects as shown in the below image.

How to Build the ASP.NET Core Web API Project in Visual Studio 2022?

Note: If you use the Build Solution option, then it will build all your projects that are present inside the solution. If you want to build a particular project, then simply right-click on the Project and select the Build option which will build that project only. The last option to build projects in Visual Studio is a Keyboard shortcut. You can use Ctrl+Shift+B to build your solution.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

To run the application, simply select the IIS option from the dropdown list and click on the IIS Express green button as shown in the below image.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

Once you click on the IIS Express button, it will run the application and you will get the following swagger page in the browser. Please have a look at the Port number (44395) on which the application is running.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

Here, the above swagger opens in the default Microsoft Edge browser. If you want then you can also change the default browser to something else like Google Chrome. To do so, simply click on the dropdown button which is appeared next to the IIS Express button as shown in the below image.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

Then select Google Chrome as the Web Browser as shown in the below image.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

With the above changes in place, now run the application and you must the web browser as Google Chrome.

Now, have a look at the port number on which the application is running. My application is running on port number 44395. Now you test the API using the same approach that we have discussed in our previous two articles. So, I am not going to discuss the same here. You just try yourself and if you face any problem, then let us know by putting a comment in the comment box. To simplify, I am just hitting the URL (https://localhost:44395/WeatherForecast) from my web browser and I am getting the result as expected as shown in the below image.

How to run the ASP.NET Core Web API Application in Visual Studio 2022?

In the next article, I am going to discuss the Default Files and Folders of the ASP.NET Core Web API Project. Here, in this article, I try to explain How to Create, Build, Run, and Test ASP.NET Core Web API Application in Visual Studio 2022 using .NET 6. I hope you enjoy this How to Create, Build, Run, and Test ASP.NET Core Web API in Visual Studio 2022 using .NET 6 article.

Leave a Reply

Your email address will not be published. Required fields are marked *