Getting Started with ASP.NET Core

Introduction to ASP.NET Core

ASP.NET Core is a free, cross-platform, open-source framework for building modern, cloud-enabled, internet-connected applications. You can build web applications, IoT applications, and mobile backends using ASP.NET Core.

Key features of ASP.NET Core include:

Prerequisites

Before you begin, ensure you have the following installed:

Note

Ensure the .NET SDK is added to your system's PATH environment variable. You can verify this by opening a terminal or command prompt and typing dotnet --version.

Installation

ASP.NET Core development is primarily done by installing the .NET SDK. There is no separate ASP.NET Core installation required.

To verify your installation, open your terminal or command prompt and run:

dotnet --version

This command should output the version of the .NET SDK you have installed.

Create Your First ASP.NET Core Project

You can create a new ASP.NET Core project using the .NET CLI. We'll start with a simple Razor Pages project.

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command to create a new Razor Pages project named MyFirstAspNetCoreApp:
    dotnet new razor -o MyFirstAspNetCoreApp
  4. Change directory into the newly created project folder:
    cd MyFirstAspNetCoreApp

Example Command

This command uses the dotnet new command with the razor template to create a new project directory named MyFirstAspNetCoreApp. The -o flag specifies the output directory.

Run Your Application

Once you have created your project, you can run it directly from the terminal.

  1. Make sure you are in your project directory (e.g., MyFirstAspNetCoreApp).
  2. Run the following command:
    dotnet run

The .NET CLI will compile and start your application. You will see output similar to this:

info: Microsoft.Hosting.Lifetime[14]
                  Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
                  Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
                  Application started. Press Ctrl+C to shut down.

Open your web browser and navigate to http://localhost:5000 or https://localhost:5001. You should see the default welcome page for your ASP.NET Core application.

Tip

Press Ctrl+C in the terminal to stop the running application.

Understanding the Project Structure

Let's briefly look at the key files and folders created in your new Razor Pages project:

For example, the Pages/Index.cshtml file is typically the home page of your application.

MVC vs. Razor Pages

ASP.NET Core offers two primary patterns for building web UIs:

Razor Pages is often recommended for beginners and for simpler web applications as it reduces the boilerplate code required by MVC.

Next Steps

Congratulations on creating and running your first ASP.NET Core application!

Here are some recommended next steps:

Continue your learning journey with the comprehensive documentation available on Microsoft Docs.