MSDN Documentation

ASP.NET Core Basics

Welcome to the fundamental guide to building web applications with ASP.NET Core. This tutorial will introduce you to the core concepts, architecture, and essential components of this powerful, cross-platform, open-source framework.

What is ASP.NET Core?

ASP.NET Core is a modern, high-performance, and flexible web development framework built by Microsoft. It enables developers to create cloud-ready, internet-connected applications for Windows, macOS, and Linux.

Key Features:

Project Structure

A typical ASP.NET Core project has a well-defined structure:


MyWebApp/
├── Controllers/
│   └── HomeController.cs
├── Models/
│   └── ErrorViewModel.cs
├── Views/
│   ├── Home/
│   │   └── Index.cshtml
│   └── Shared/
│       └── _Layout.cshtml
├── appsettings.json
├── Program.cs
└── Startup.cs
            

The Role of `Startup.cs`

The Startup.cs file is crucial. It contains two main methods:

ASP.NET Core Architecture Diagram

Middleware Pipeline

The request pipeline is a series of middleware components, each responsible for handling a specific aspect of the request or response. Middleware is executed in the order it's added to the pipeline.

Example configuration in Startup.cs:


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles(); // Serves static files like HTML, CSS, JS

    app.UseRouting(); // Enables attribute routing and other route matching

    app.UseAuthorization(); // Adds authorization to the pipeline

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
            

Model-View-Controller (MVC)

ASP.NET Core MVC is a popular pattern for organizing web applications:

Razor Pages

For simpler page-based applications, Razor Pages provide an alternative to the full MVC pattern, offering a more focused approach to building Razor components.

Next Steps

This tutorial has provided a glimpse into the core concepts. To continue your journey:

Continue to Advanced Topics