Back to: ASP.NET Core Web API Tutorials
Creating ASP.NET Core Web API Project in Visual Studio
In this article, I will discuss How to Create, Build, Run, and Test the ASP.NET Core Web API Project in Visual Studio. Please read our previous article discussing How to Create, Build, Run, and Test the ASP.NET Core Web API project using .NET Core CLI and Visual Studio Code.
Creating ASP.NET Core Web API Project Using Visual Studio
Now, we will see the step-by-step process to create the ASP.NET Core Web API project using Visual Studio 2022. So, open Visual Studio 2022 and click on the Create a new project option, as shown in the image below.
Once you click on the Create a new project option, it will open the following Create a new project window. Here, you need to select the “ASP.NET Core Web API” template, which uses C# as the programming language, and then click the Next button, as shown in the image below.
Once you click the Next button, 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. Finally, you need to click on the Next button, as shown in the image below.
Once you click on the Next button, it will open the Additional Information window. Here, you need to select the Target .NET version. The authentication Types. Whether you want to configure HTTPS and enable Docker, etc. We will use .NET 8, which provides Long Term Support (LTS), so select .NET 8 as the Framework. We will not use any authentication now, so select the authentication type None. Then, apart from Enable Docker, check 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.
The meaning of the above checkboxes is as follows:
- Configure for HTTPS: This option configures your ASP.NET Core application to use HTTPS (Hypertext Transfer Protocol Secure) by default. HTTPS ensures that the communication between the client and server is encrypted, enhancing security. When you select this option, Visual Studio sets up the necessary SSL (Secure Sockets Layer) certificates and configures the application to listen on an HTTPS endpoint.
- Enable Docker Support: Selecting this option adds a Dockerfile to your project and configures it for Docker containerization. Docker is a platform used for developing, shipping, and running applications inside containers. Enabling Docker support means your application can be easily packaged and deployed as a container, ensuring consistency across environments.
- Use Controllers: This option structures your Web API project to use MVC (Model-View-Controller) controllers. Controllers are classes that handle incoming HTTP requests and return responses. By using controllers, you can organize your API logic into different actions within these classes, providing a clean separation of concerns and an organized codebase.
- Enable OpenAPI Support: OpenAPI, also known as Swagger, is a specification for building APIs. Enabling OpenAPI support automatically generates documentation for your API, provides a UI for testing API methods, and helps with client generation. This is especially useful for larger projects or APIs that will be consumed by various clients, as it makes understanding and interacting with the API much easier.
- Do Not Use Top-Level Statements: In recent versions of C#, top-level statements allow for a more simplified coding structure, especially for small applications or scripts, by reducing code. However, for larger, more complex applications, you might prefer the traditional structure with explicit Program class and Main method. Selecting “Do Not Use Top-Level Statements” opts for this more traditional, explicit structuring of the entry point in your application.
Once you click on the Create button, Visual Studio will create the ASP.NET Core Web API project with the following file and folder structure. In our next article, we will discuss all these files and folders in detail.
That’s it! You have created an ASP.NET Core API project using Visual Studio 2022 with ASP.NET Core 8.0. You can continue building your API by adding more controllers, models, and services as required.
How do you Build the ASP.NET Core Web API Project in Visual Studio?
You can build the ASP.NET Core Web API Project in many ways. So, let us discuss them. From the Visual Studio menus, select Build => Build Solution, as shown in the below image.
Once you select Build => Build Solution, it will build all the projects in the solution. Once the build is successful, you will get the below message in the output.
You can also build your project by right-clicking on it and then selecting the build option from the context menus. You can also right-click on your solution and select the Build Solution option to build all your projects. The last option for building projects in Visual Studio is a keyboard shortcut. You can use Ctrl+Shift+B to build your solution.
Note: Using the Build Solution option will build all your projects inside the solution. If you want to build a particular project, right-click on the Project and select the Build option, which will only build that project.
How do you run the ASP.NET Core Web API Application in Visual Studio?
You can run the application in Visual Studio using the HTTP, HTTPS, WSL, and IIS Express options, as shown in the below image. So, from the launch profile, select the https option and click on the https button, as shown in the image below.
Differences Between HTTP, HTTPS, WSL, and IIS Express in Visual Studio
HTTP (Hypertext Transfer Protocol):
- HTTP is a protocol used for transmitting data over the internet, primarily for webpages.
- It operates at the application layer and facilitates data transfer between web servers and clients (browsers).
- HTTP does not encrypt the data, which makes it less secure. Third parties can intercept sensitive data.
HTTPS (HTTP Secure):
- HTTPS is the secure version of HTTP. It employs SSL/TLS encryption to secure the data transfer.
- This encryption ensures that the data exchanged between the server and the client is encrypted and secure from interception or tampering.
- For web applications, especially those handling sensitive data like login credentials and payment information, HTTPS is a must to ensure data security and integrity.
WSL (Windows Subsystem for Linux):
- WSL is a feature in Windows that allows users to run a Linux environment directly on Windows without needing a virtual machine or dual-boot setup.
- It is useful for developers who need to run Linux-based applications, tools, or development environments on a Windows machine.
- In the context of .NET development, WSL allows developers to test and run .NET applications in a Linux environment while using Windows as their primary OS.
IIS Express:
- IIS Express is a lightweight, self-contained version of IIS (Internet Information Services) optimized for developers.
- It is used within Visual Studio to develop and test web applications locally.
- IIS Express supports both HTTP and HTTPS and is designed to make it easy to develop and test web applications without requiring administrative privileges or a full IIS installation.
Once you click on the https button, the application will run, and you will get the following swagger page in the browser. Please have a look at the Port number (7237) on which the application is running. The port number might be different on your machine.
Now, have a look at the port number on which the application is running. Now, you can test the API using the same approach we discussed in our previous two articles. So, I am not going to discuss this here. You can try it yourself, and if you face any problems, then let me know by commenting in the comment box. To simplify, I am just hitting the URL (https://localhost:7237/WeatherForecast) from my web browser, and I am getting the result as expected, as shown in the image below.
In the next article, I will discuss the Default Files and Folders of the ASP.NET Core Web API Project. In this article, I explain How to Create, Build, Run, and Test ASP.NET Core Web API Application in Visual Studio 2022 using .NET 8. I hope you enjoy this article on how to create, build, run, and test ASP.NET Core Web API in Visual Studio 2022.