Creating ASP.NET Core Web Application using Visual Studio

Creating ASP.NET Core Web Application using Visual Studio

In this article, I will discuss How to Create the ASP.NET Core Web Application using Visual Studio 2022. Please read our previous article discussing the Differences Between .NET Core and .NET Framework.

Creating the First ASP.NET Core Web Application using Visual Studio 2022

Now, we will create the ASP.NET Core Application using .NET 8. To create a new ASP.NET Core Application using .NET 8, open Visual Studio 2022 and click the Create a new project box, as shown in the image below.

Creating the First ASP.NET Core Web Application using Visual Studio 2022 and .NET 6

Once you click on the Create a new project box, the “Create a new project” window will open. This window includes different .NET 8 application templates. Here, we will create a simple Web application from scratch, so select the ASP.NET Core Empty project template and click the Next button, as shown in the image below.

Creating ASP.NET Core Web Application using .NET 6

What is an ASP.NET Core Empty Project Template?

The ASP.NET Core Empty template selection indicates that we are creating an empty ASP.NET Core project. This template comes with a minimal setup, i.e., with the basic structure for an ASP.NET Core project, without pre-configured controllers, views, or APIs.

Once you click on the Next button, it will open the following Configure Your New Project window. Here, you need to provide the necessary information to create a new project. First, give an appropriate name for your project (FirstCoreWebApplication), set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Create button, as shown in the image below.

Creating ASP.NET Core Web Application using .NET 6

Understanding Configure Your New Project Window:
Project name:

This is the field where we need to specify the name of our new project. Here, we specified the project name as FirstCoreWebApplication. The project name will also be used as the default namespace in our code files. It should be descriptive and unique to easily identify the project within our solution.

Location:

The location text box specifies the directory where the project will be created on our local system. In this case, the project location is set to “D:\Projects”. You can choose a different folder by either typing the path or clicking the … button to browse to a directory of your choice.

Solution name:

A solution is a container for one or more projects, and the solution name usually matches the project name when you’re working on a single project. Visual Studio organizes multiple related projects under a solution for ease of management. By default, the solution name is the same as the project name, but you can modify it if your solution contains multiple projects. Here, the Solution name field contains “FirstCoreWebApplication”.

Place solution and project in the same directory:

If you check this box, Visual Studio will store both the solution and project files in the same directory. By default, this checkbox is unchecked, and Visual Studio creates a separate folder for the solution and then a subfolder for the project inside that solution folder. This approach is useful if you plan to add multiple projects to the same solution in the future.

Project will be created in:

This label shows the full path on which the project will be created. In our case, the project will be created in: D:\Projects\FirstCoreWebApplication\FirstCoreWebApplication\

The first folder, FirstCoreWebApplication, is the solution folder, and the second FirstCoreWebApplication is the project folder inside the solution folder.

In the above window, clicking on the Next button will open the Additional Information window. Here, you need to select .NET 8 as the Framework, check the Configure for HTTPS, Do not use top-level statements check boxes, and finally, click the Create button, as shown in the image below.

Creating ASP.NET Core Web Application using Visual Studio

Understanding the Additional Configuration Window:
Framework

This dropdown allows us to select the version of .NET you want to target. Here, we need to select .NET 8.0 (Long-Term Support). This means we are building the project using .NET 8.0, the latest version of the .NET platform, with long-term support (LTS). LTS releases are maintained for a longer period, typically three years, making them suitable for production applications.

Configure for HTTPS

This checkbox is used to configure your project to use HTTPS. When this option is checked, indicating that the project will be configured to use HTTPS by default, which is essential for secure communication between the server and clients. It is highly recommended for production environments. Visual Studio will add the necessary SSL settings to your ASP.NET Core project here.

Enable Container Support

This option allows us to add Docker support to our project, enabling containerization. This option is unchecked, meaning containerization support is not enabled for the project. If checked, we could run the project inside a container such as Docker, useful for microservices, cloud-based deployments. If enabled, we also have to select the container OS.

Container OS

This dropdown becomes available if “Enable container support” is checked. It lets us choose the operating system the container will use, either Linux or Windows. As you can see by default, Linux is selected as the Container OS, but it is disabled since Enable container support is unchecked, which is a common choice for containers because of its lightweight nature.

Container Build Type

This dropdown also becomes available if “Enable container support” is checked. It allows us to choose between Dockerfile and .NET SDK. As you can see by default, Dockerfile is selected as the build type but is also disabled since container support is not enabled. When enabled, this option specifies that a Dockerfile will be used to build the container image.

Do not use top-level statements

Checking this option means that the project will not use top-level statements. Top-level statements are a feature introduced in C# 9.0 that simplifies the program structure by allowing us to omit the Main method when writing simple applications. Checking this box means we will follow the traditional program structure, where the Main method is explicitly defined.

Enlist in .NET Aspire Orchestration

This option is related to Azure or a DevOps process. If unchecked, the project will not be part of the .NET Aspire orchestration. This orchestration feature is provided in certain environments for managing complex deployments, particularly for Azure or cloud-based deployment automation.

So, once you click on the Create button, a new ASP.NET Core Web Application will be created in Visual Studio 2022 using .NET 8. The project will have the following file and folder structure.

How to Create the ASP.NET Core Web Application using Visual Studio 2022 and .NET 6

Understanding the Project Files:
Connected Services:

This folder provides a way for our application to connect to external services like Azure, REST APIs, WCF services, or Database connections. It helps us integrate external functionality into our application without manual configurations. Currently, it’s empty because no services have been added yet.

Dependencies:

This folder manages all the NuGet packages, SDKs, .NET libraries, and any third-party libraries that our project depends on. That means it includes both built-in ASP.NET Core libraries and third-party libraries you might add later to enhance your application’s functionality. By default, the basic ASP.NET Core dependencies, such as Microsoft.NETCore.App and Microsoft.AspNetCore.App, will be included.

Microsoft.NETCore.App

This metapackage provides the essential runtime components and libraries needed for any .NET Core application to run. When creating any type of .NET Core application (such as console apps, Windows services, etc.), we will use the libraries included in this package. This package provides the following features:

  • .NET Core Runtime: Includes the runtime libraries required to execute .NET applications.
  • Core Libraries: Provides fundamental libraries such as System.Collections, System.IO, System.Linq, System.Threading, and more. These libraries are the foundation for all .NET Core applications.
  • Base Class Libraries (BCL): The BCL provides a standard set of classes that include primitive data types, collections, file I/O, and other basic functionalities required to build applications.
Microsoft.AspNetCore.App

This metapackage is specific to ASP.NET Core applications, providing the necessary libraries and tools to create web applications, RESTful APIs, and server-side services. When we create an ASP.NET Core application (web apps, APIs, etc.), we will use this metapackage. This package provides the following features:

  • NET Core Framework: It includes all the core libraries required to build web applications, such as MVC, Razor Pages, API controllers, etc.
  • Entity Framework Core: Provides the default Object Relational Mapping (ORM) library, which allows you to interact with databases.
  • Middleware Components: It contains built-in middleware components, like routing, authentication, authorization, static file handling, logging, and more, which are essential for ASP.NET Core web applications.
  • Dependency Injection and Configuration: Provides support for configuring services and dependency injection, which are core concepts in ASP.NET Core applications.
  • Identity: Includes components for implementing authentication, such as ASP.NET Core Identity for managing users, roles, and sign-ins.

So, Microsoft.NETCore.App provides the core functionality needed to run any .NET Core application. Microsoft.AspNetCore.App extends Microsoft.NETCore.App and add ASP.NET Core-specific libraries and functionality that are essential for building and running web applications.

What Are Analyzers?

Analyzers are tools integrated into Visual Studio that analyze your source code while you are writing it. They help ensure code quality, identify potential problems, enforce coding standards, identify bugs, and provide improvement suggestions. They run in the background while you code and generate warnings or errors in the editor if they find something that doesn’t conform to certain coding standards or best practices.

Properties:

This folder contains the launchSettings.json file, configuring how our application starts during development. It defines the environment, URLs, and profiles (IIS Express, Kestrel, etc.) used to run the project locally using Visual Studio.

appsettings.json:

This is a configuration file where we can define key-value pairs for settings used throughout our application. It typically includes connection strings, logging configurations, API keys, or any other application-level configuration. It follows a JSON structure and provides a centralized location for managing configurations that can be easily accessed throughout the project.

Program.cs

This is the entry point of our ASP.NET Core application. The Program.cs file contains the Main method, which is where our application starts. It also creates the host for the application. This file configures the web server (Kestrel or IIS) and the necessary middleware components that handle requests.

Run ASP.NET Core Application:

To run this ASP.NET Core Web Application, click on IIS Express or press F5 (with debugging) or Ctrl + F5 (without debugging). This will open the browser and display the following output.

Run ASP.NET Core Application

Here, the output “Hello World!” comes from the Main method of the Program class, which is present inside the Program.cs file, as shown in the below image.

Run ASP.NET Core Application

Now, open the Program.cs class file and change the “Hello World!” string to something else, as shown in the code below. Rerun the application, and the output will change accordingly.

namespace FirstCoreWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();

            app.MapGet("/", () => "Welcome to .NET 8");

            app.Run();
        }
    }
}
Project Templates in ASP.NET Core

As you can see in the below image, while creating ASP.NET Core Application, we have different types of project templates for creating ASP.NET Core Web applications.

Let us understand what all these project templates are and their use.

ASP.NET Core Web App (Razor Pages)
  • The ASP.NET Core Web App project template uses Razor Pages, which is a page-based programming model for building Web UIs. Razor Pages simplifies the development of page-focused scenarios by allowing developers to define page-specific logic within the same file as the HTML markup.
  • It is suitable for building web applications with a simpler structure, where each page is self-contained and designed to handle requests and responses directly.

ASP.NET Core Empty

  • The ASP.NET Core Empty project template creates a minimal ASP.NET Core application with no predefined folders or files. It allows developers to add only the components and middleware they need.
  • It is ideal for advanced users who want complete control over the project structure and dependencies or for creating highly customized applications from scratch.
ASP.NET Core Web API
  • The ASP.NET Core Empty project template is designed to create RESTful HTTP services. It includes predefined folders and files for creating and managing API endpoints and is configured with the necessary middleware for building Web APIs.
  • It is best suited for building backend services that expose data and functionality through HTTP endpoints intended for consumption by client applications such as web, mobile, or desktop apps.
ASP.NET Core Web API (Native)
  • The ASP.NET Core Web API (Native) project template is similar to the standard Web API template but optimized for running natively on various platforms. It includes additional configurations and dependencies to use native platform capabilities.
  • It is useful for developers looking to build APIs that can be deployed and run natively on specific platforms with optimized performance.
ASP.NET Core Web App (Model-View-Controller)
  • This project template uses the Model-View-Controller (MVC) architectural pattern. It separates an application into three main components: Models (data), Views (UI), and Controllers (business logic).
  • It is suitable for building complex web applications with a clear separation of concerns, where the application logic, UI, and data access layers are distinct.
Blazor Server App
  • The Blazor Server App Project template creates a Blazor Server application. Blazor Server allows for the development of interactive web UIs using C# instead of JavaScript. The app runs on the server, with UI updates sent to the client via SignalR.
  • It is ideal for building real-time, interactive web applications where the client-side logic needs to be executed on the server, ensuring consistent behavior and reducing client-side resource requirements.
Blazor WebAssembly App
  • The Blazor WebAssembly App Project template creates a Blazor WebAssembly application. Blazor WebAssembly runs C# code directly in the browser using WebAssembly. The entire app, including .NET runtime, is downloaded and executed on the client side.
  • It is suitable for building single-page applications (SPAs) that run entirely in the browser, providing a rich, responsive user experience similar to JavaScript frameworks like Angular or React.
Razor Class Library
  • The Razor Class Library project template creates a library containing reusable Razor UI components. These components can be shared across multiple projects, promoting code reuse and modularity.
  • It is ideal for developing UI components, such as layouts, partial views, and Razor Pages, that can be packaged and reused in different ASP.NET Core applications.

Note: In our coming articles, we will discuss how and when to use some of the above project templates.

In the next article, I will discuss the ASP.NET Core Project File. In this article, I try to explain how to create the ASP.NET Core Web Application using Visual Studio 2022 and .NET 8. I would like to have your feedback. Please post feedback, questions, or comments about this Creating ASP.NET Core Web Application using Visual Studio 2022 and .NET 6 article.

3 thoughts on “Creating ASP.NET Core Web Application using Visual Studio”

  1. In the introduction to the ASP.NET Core Web API, the opening sentence is wrong, not “The ASP.NET Core Empty project template”.

Leave a Reply

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