🚀

MSDN Tutorials

Getting Started with ASP.NET Core

Welcome to the essential guide for starting your journey with ASP.NET Core! This tutorial series will walk you through the fundamental concepts and steps to build your first web application using this powerful, cross-platform framework.

Prerequisites

Before you begin, ensure you have the following installed:

Step 1: Create Your First ASP.NET Core Project

We'll start by creating a basic web application. You can do this using the .NET CLI or your IDE.

Using the .NET CLI:

Open your terminal or command prompt and run the following commands:

dotnet new webapp -o MyFirstAspNetCoreApp
cd MyFirstAspNetCoreApp

This creates a new directory named MyFirstAspNetCoreApp containing your project files.

Using Visual Studio:

  1. Open Visual Studio.
  2. Click "Create a new project".
  3. Search for "ASP.NET Core Web App" and select it.
  4. Click "Next".
  5. Enter your project name (e.g., MyFirstAspNetCoreApp) and click "Next".
  6. Choose the target framework and click "Create".

Step 2: Understand the Project Structure

Your new project will have a directory structure like this:

MyFirstAspNetCoreApp/
├── MyFirstAspNetCoreApp.csproj
├── Pages/
│   ├── Index.cshtml
│   ├── Privacy.cshtml
│   └── _Layout.cshtml
│   └── _ValidationScriptsPartial.cshtml
│   └── Error.cshtml
│   └── _ViewStart.cshtml
│   └── _ViewImports.cshtml
├── Properties/
│   └── launchSettings.json
├── appsettings.Development.json
├── appsettings.json
├── Program.cs
└── README.md

Key files include:

  • Program.cs: The entry point of your application.
  • Pages/Index.cshtml: The main page of your web app.
  • .csproj: The project file that defines your project's dependencies and configuration.

Step 3: Run Your Application

Let's see your application in action!

Using the .NET CLI:

Navigate to your project directory in the terminal and run:

dotnet run

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

Using Visual Studio:

Click the "Run" button (often a green play icon) or press F5.

Tip: You can change the default port in the Properties/launchSettings.json file.

Step 4: Explore the Razor Pages Model

ASP.NET Core often uses Razor Pages for building web UIs. Each .cshtml file represents a page, and it can optionally have a corresponding .cshtml.cs file for the page's logic (the "code-behind").

For example, Pages/Index.cshtml might have a corresponding Pages/Index.cshtml.cs file which inherits from PageModel. This model can handle data binding, page logic, and interact with services.

Next Steps

Congratulations! You've successfully created and run your first ASP.NET Core application. Continue exploring these topics: