Lesson 2
Creating Your First ASP.NET Core Application
Introduction

Welcome! This lesson will guide you through the process of creating your first ASP.NET Core application. By the end, you'll have a basic web app that displays "Hello, World!" and a clear understanding of the core files within your project.

Using .NET CLI to Create a New Minimal API

As mentioned in the previous lesson, you can use the .NET CLI to create a basic app by specifying the desired template. To create a minimal API application with ASP.NET Core, run the following command:

Bash
1dotnet new web -o MyTodoApp

This command sets up the basic files required to build and run your first ASP.NET Core application.

Building the Application

Before running your application, ensure all project dependencies are downloaded to your machine. While the .NET CLI automatically restores packages when creating your project, it's helpful to understand the process. You can use the following command to manually restore dependencies:

Bash
1dotnet restore

Run this command inside your project folder to restore the necessary packages using the NuGet package manager. NuGet is the library package manager for .NET, where libraries are packaged in NuGet packages and published to nuget.org.

Running the Application

To run the application from the command line using the .NET CLI tools, use the following command:

Bash
1dotnet run

This will start the server on the default port 5000. In the CodeSignal IDE, applications run on port 3000 as the preview pane is attached to this specific port. Make sure to execute this command inside your project folder.

The steps for creating, building, and running the application are provided for your understanding. In our environment, the server starts automatically in watch mode on port 3000, so you don't need to perform the dotnet new, dotnet restore, and dotnet run steps manually. We always create the project for you to work on.

The .csproj Project File

The .csproj file is a crucial part of your ASP.NET Core project and serves as the project file for .NET applications. It contains all the metadata needed to build your project, specifying the type of application, the target framework, and dependencies.

Here is an example of a .csproj file:

HTML, XML
1<Project Sdk="Microsoft.NET.Sdk.Web"> 2 3 <PropertyGroup> 4 <TargetFramework>net6.0</TargetFramework> 5 </PropertyGroup> 6 7 <ItemGroup> 8 <PackageReference Include="Microsoft.AspNetCore.App" /> 9 </ItemGroup> 10 11</Project>
  • Project Sdk: Specifies the SDK to use for building the project. Here, Microsoft.NET.Sdk.Web is used for web applications.
  • PropertyGroup: Contains various properties for the project. The <TargetFramework> property defines the .NET platform version (e.g., net6.0).
  • ItemGroup: Lists dependencies required by the project. For instance, <PackageReference Include="Microsoft.AspNetCore.App" /> specifies a package reference.

To add new libraries or NuGet packages, you can use the .NET CLI. For example:

Bash
1dotnet add package Newtonsoft.Json

This command adds the Newtonsoft.Json package to your project. You can see available NuGet packages at nuget.org. You can use these packages in your project by referencing the unique package name in your .csproj file, making the package’s namespace and classes available in your code files.

The .csproj file is essential for organizing your project and ensuring that .NET CLI have the necessary information to build your app.

Program.cs File

Program.cs is the entry point of your ASP.NET Core application, where the web server and the application are configured and started. Below is an example of how to set up a basic "Hello, World!" application in ASP.NET Core:

C#
1var builder = WebApplication.CreateBuilder(args); 2var app = builder.Build(); 3 4app.MapGet("/", () => "Hello, World!"); 5 6app.Run();

Let's break this down line by line:

  1. Create the builder:

    C#
    1var builder = WebApplication.CreateBuilder(args);

    This initializes a new WebApplicationBuilder, which is responsible for configuring the web server, loading configuration settings, setting up logging, and registering necessary services. By default, the WebApplicationBuilder comes with several pre-configured settings to get you started quickly.

  2. Build the application:

    C#
    1var app = builder.Build();

    This creates a WebApplication instance from the WebApplicationBuilder. This instance is used to define the behavior of the application, including handling incoming HTTP requests and generating responses.

  3. Map the default route:

    C#
    1app.MapGet("/", () => "Hello, World!");

    This line defines an endpoint that listens for GET requests at the root URL (/). When a request is received at this URL, the application responds with "Hello, World!".

  4. Run the application:

    C#
    1app.Run();

    This starts the application and begins listening for incoming HTTP requests using the Kestrel web server.

Within the WebApplication, two main concepts control the flow of requests: middleware and endpoints.

  • Middleware: These are small components executed in sequence for each HTTP request. Middleware can perform tasks such as logging, serving static files, and handling exceptions. In this minimal example, we don't explicitly add any middleware, but many default middleware components are configured automatically by the WebApplicationBuilder.

  • Endpoints: Endpoints define how specific requests should be handled. For instance, MapGet handles GET requests and responds with a specified output. There are other methods to map different HTTP verbs, like MapPost, MapPut, and MapDelete.

By setting up a basic "Hello, World!" application, you now have a foundational understanding of how an ASP.NET Core application is structured and how the main components interact.

appsettings.json

appsettings.json holds configuration settings for your ASP.NET Core application that are used at runtime to control the behavior of your app. By default, this file is located in your project's root directory and looks something like this:

JSON
1{ 2 "Logging": { 3 "LogLevel": { 4 "Default": "Information", 5 "Microsoft.AspNetCore": "Warning" 6 } 7 }, 8 "AllowedHosts": "*" 9}

These settings configure logging levels and allowed hosts for the application.

You can have multiple configuration files for different environments, such as appsettings.Production.json, appsettings. Development.json, and appsettings.Sandbox.json. These files enable you to manage settings specific to each environment, making it easier to handle configurations for development, testing, and production scenarios. ASP.NET Core automatically selects the appropriate settings file based on the current environment.

Summary

In this lesson, you learned how to create a new ASP.NET Core project using the .NET CLI and became familiar with the basic structure of an ASP.NET Core project. We covered key files such as the .csproj project file, which manages your project's metadata and dependencies, the Program.cs file, which serves as the entry point of your application, and the appsettings.json file, which holds configuration settings. With this foundational knowledge, you're now ready to dive into a simple practice exercise that we have prepared for you.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.