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:
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:
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:
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:
- Project Sdk: Specifies the SDK to use for building the project. Here,
Microsoft.NET.Sdk.Webis 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:
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.
