Getting Started with ASP.NET Core

Welcome to the ASP.NET Core getting started guide! This tutorial will walk you through the fundamental steps of creating your first ASP.NET Core web application. ASP.NET Core is a modern, cross-platform, open-source framework for building internet-connected applications, such as web apps, IoT apps, and mobile backends.

Prerequisites

Before you begin, ensure you have the following installed:

  • .NET SDK: Download and install the latest .NET SDK from the official .NET website.
  • Code Editor: Visual Studio Code with the C# extension is highly recommended, but any code editor will suffice.

Creating Your First ASP.NET Core App

We'll use the .NET CLI (Command Line Interface) to create a new web application. Open your terminal or command prompt and navigate to the directory where you want to create your project.


dotnet new webapp -o MyFirstAspNetCoreApp
cd MyFirstAspNetCoreApp
                

This command does the following:

  • dotnet new webapp: Creates a new ASP.NET Core web application project.
  • -o MyFirstAspNetCoreApp: Specifies the output directory for the project.
  • cd MyFirstAspNetCoreApp: Changes the current directory to your new project folder.
ASP.NET Core Project Structure

Understanding the Project Structure

Inside the MyFirstAspNetCoreApp directory, you'll find several files and folders. Key ones include:

  • Pages/: Contains Razor Pages. Each .cshtml file represents a page in your application.
  • wwwroot/: The web-accessible root folder for static files like CSS, JavaScript, and images.
  • appsettings.json: Configuration settings for your application.
  • Program.cs: The entry point of your application, where the host is built and configured.
  • Startup.cs (in older .NET Core versions) or configuration within Program.cs (in .NET 6+): Configures services and the HTTP request pipeline.

Running Your Application

To run your application, use the following command in your project's root directory:


dotnet run
                

The output will show you the URL(s) where your application is running. Typically, it's http://localhost:5000 or https://localhost:5001. Open this URL in your web browser.

Tip: You can also run your application directly within Visual Studio or Visual Studio Code by clicking the "Run" or "Debug" button.

Exploring the Default Page

You should see a default ASP.NET Core welcome page. This page is usually generated from Pages/Index.cshtml. Let's look at a simplified example of what you might find:


@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
                

The @page directive indicates this is a Razor Page. The @model directive links it to its code-behind model (Pages/Index.cshtml.cs), which contains page-specific logic and data.

Next Steps

Congratulations! You've successfully created and run your first ASP.NET Core application. From here, you can explore:

  • Creating new Razor Pages or MVC views.
  • Adding controllers and actions for API endpoints.
  • Working with data using Entity Framework Core.
  • Implementing authentication and authorization.
Note: ASP.NET Core has evolved significantly. For the latest features and best practices, always refer to the official Microsoft documentation.