What ASP.NET Core Is

ASP.NET Core is the .NET web framework: a built-in server, an ordered request pipeline, and the endpoints you write to answer URLs.

Syntaxdotnet new web -o MyApi

A web server is a program that waits on a network port, reads HTTP requests and writes HTTP responses. HTTP is the text-based protocol that browsers and other programs use to ask for things over a network. ASP.NET Core is the part of .NET that lets you write such a program.

It hands you three pieces. A server, called Kestrel, which deals with the network connections and parses HTTP for you. A pipeline: an ordered list of small components, each one given the request on the way in and the response on the way out. And endpoints: the methods you write that actually answer a particular URL.

What it replaced

The older framework was ASP.NET on .NET Framework, which included Web Forms and ASP.NET MVC 5. It was built on a library called System.Web, which was tied to IIS, which runs only on Windows. ASP.NET Core dropped that dependency. The server is a library running inside your own process, so the whole application is one ordinary executable that you start like any other program. That is why it runs on Linux, and why it fits a container, where one process is the unit of deployment.

What trips people up

Searching for "ASP.NET" still returns a great deal of .NET Framework material: .aspx pages, Global.asax, web.config. None of it applies here. Search for "ASP.NET Core", and check that the version selector on the documentation page reads 10.0.

Example

Example · csharp
// The whole of Program.cs for a project created with: dotnet new web
// No using directives are needed; the Web SDK supplies the common ones.

var builder = WebApplication.CreateBuilder(args);

// Build() turns everything configured on the builder into a running app.
var app = builder.Build();

// An endpoint: a GET request for "/" is answered by this method.
app.MapGet("/", () => "ASP.NET Core is listening.");

// Returning an object instead of a string makes the response JSON.
app.MapGet("/status", () => new { Service = "demo", Ready = true });

// Starts Kestrel and blocks until the process is told to stop.
app.Run();

When to use it

  • Decide whether a tutorial you found applies to .NET 10 or to the old .NET Framework ASP.NET.
  • Explain to a team why a .NET web service can be deployed as a single Linux container with no IIS.
  • Create the smallest possible HTTP service to confirm that a port, firewall rule or ingress is wired up.

More examples

The project file that makes it a web app

One attribute separates a web application from a console application: the SDK is Microsoft.NET.Sdk.Web rather than Microsoft.NET.Sdk. That SDK brings in the ASP.NET Core framework reference, so there is no package to install, and it is what adds the implicit using directives for Microsoft.AspNetCore.Builder, Microsoft.AspNetCore.Http and the rest. Reach for this when you are checking why WebApplication cannot be found in an existing project.

Example · xml
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

Answering a request without any endpoint helper

This shows what the framework is doing on your behalf. The object named context carries the request and the response; MapGet and JSON serialisation are conveniences built on top of writing to it. A request for /anything comes back as the plain text "you asked for /anything". You would write code at this level when building something that is not a normal endpoint, such as a health probe that must answer before the rest of the application is ready.

Example · csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// app.Run with a delegate is the raw form underneath MapGet: no URL
// matching, no serialisation. It answers every request, whatever the
// path or the method, by writing bytes to the response itself.
app.Run(async context =>
{
    context.Response.StatusCode = 200;
    context.Response.ContentType = "text/plain; charset=utf-8";
    await context.Response.WriteAsync($"you asked for {context.Request.Path}");
});

// A different method with the same name: this one starts the server.
app.Run();

Discussion

  • Be the first to comment on this lesson.