Introduction to ASP.NET Core

Welcome to the introductory tutorial for ASP.NET Core. This guide will provide you with a foundational understanding of what ASP.NET Core is, its key features, and how to get started with building modern, cross-platform web applications.

What is ASP.NET Core?

ASP.NET Core is an open-source, cross-platform framework for building modern, cloud-based, internet-connected applications. It is a re-architecture of ASP.NET that runs on the .NET Core platform. ASP.NET Core can be used to build:

  • Web applications
  • IoT applications
  • Mobile backends
  • Microservices
  • RESTful APIs

Key Features

  • Cross-Platform: Develop and deploy on Windows, macOS, and Linux.
  • Open-Source: The entire ASP.NET Core stack is open-source and community-driven.
  • High Performance: Built for performance, with a focus on efficiency and throughput.
  • Unified MVC and Web API: A single programming model for building web UIs and APIs.
  • Dependency Injection: Built-in support for dependency injection, making applications more modular and testable.
  • Tag Helpers: Server-side Razor code written in C# that can modify HTML elements in the markup.
  • Blazor: A framework for building interactive client-side web UIs with .NET.

Getting Started

To start building with ASP.NET Core, you'll need to have the .NET SDK installed. You can download it from the official .NET download page.

Once the SDK is installed, you can create a new project using the command line:

dotnet new webapp -o MyWebApp
cd MyWebApp
dotnet run

This command will create a new ASP.NET Core web application project named 'MyWebApp', navigate into its directory, and then run the application. You can then open your web browser and navigate to http://localhost:5000 (or the port indicated in the output) to see your new application.

Project Structure

A typical ASP.NET Core web application project has the following structure:

MyWebApp/
├── Controllers/
├── Pages/              (For Razor Pages)
├── Views/              (For MVC Views)
├── wwwroot/            (Static files)
├── appsettings.json    (Configuration)
├── Program.cs          (Application entry point)
├── Startup.cs          (Application configuration - older versions)
└── ...

In modern ASP.NET Core (typically .NET 6 and later), the `Program.cs` file contains the minimal API hosting setup, often including the bootstrapping of the application's services and request pipeline configuration.

Program.cs Example (Minimal API)

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
// Or builder.Services.AddControllersWithViews(); for MVC

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

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

app.Run();

Next Steps

This introduction has provided a glimpse into ASP.NET Core. In the next tutorials, we will dive deeper into specific areas like setting up your development environment, understanding the project structure, building Razor Pages, working with the Model-View-Controller (MVC) pattern, and creating Web APIs.

Continue to the Getting Started tutorial to learn how to set up your development environment and create your first ASP.NET Core application.