MSDN Documentation

Creating a Web Application with .NET Core

This tutorial guides you through the process of building a simple web application using .NET Core. We'll cover project setup, basic routing, and rendering content.

Prerequisites

Before you begin, ensure you have the following installed:

Step 1: Create a New Project

1

Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:

dotnet new webapp -o MyWebApp

This command creates a new web application project named MyWebApp in a new directory.

Step 2: Explore the Project Structure

2

Navigate into the newly created project directory:

cd MyWebApp

You'll find several files and folders. Key ones include:

  • Pages/: Contains Razor Pages, which are used for creating the UI.
  • wwwroot/: Contains static files like CSS, JavaScript, and images.
  • Program.cs: The entry point of your application.
  • appsettings.json: Application configuration settings.

Step 3: Run the Application

3

To run the application, execute the following command in your terminal:

dotnet run

This will build and start the web server. You'll see output indicating the URLs where your application is listening. Typically, it's https://localhost:5001 and http://localhost:5000.

Open one of these URLs in your web browser to see the default welcome page.

Step 4: Creating a New Page

4

Let's create a simple "About" page. In the Pages/ directory, create a new file named About.cshtml. Add the following content:

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

About This Application

This is a sample web application created with .NET Core.

You'll also need to add a corresponding About.cshtml.cs file in the same directory:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MyWebApp.Pages
{
    public class AboutModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}

Restart the application using dotnet run. You can now access the "About" page by navigating to https://localhost:5001/about (or the equivalent HTTP URL).

Next Steps

You've successfully created and run a basic web application with .NET Core. From here, you can explore: