ASP.NET Core 8 Website - Plain Text To HTML Home Page

PART 2: Converting Plain Text into an HTML Home Page

PART 1 returned plain text from /. This part returns a complete HTML5 document and inserts the current server time into it.

1. Stop the application

Press Ctrl+C in the terminal.

2. Replace Program.cs

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

app.MapGet("/", () =>
{
    string serverTime = DateTime.Now.ToString("HH:mm:ss");

    string html = $$"""
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport"
                  content="width=device-width, initial-scale=1.0">
            <title>Home - basicdotnet</title>
        </head>
        <body>
            <h1>Welcome to basicdotnet</h1>
            <p>A simple ASP.NET Core 8 website.</p>
            <p>It's {{serverTime}} on the server.</p>
        </body>
        </html>
        """;

    return Results.Content(html, "text/html");
});

app.Run();

3. Understand the response

Results.Content(html, "text/html") tells the browser to render the returned string as HTML. The C# raw string literal uses triple quotation marks, making multiline HTML easier to read.

The two dollar signs in $$""" enable interpolation with double braces:

{{serverTime}}

The value is calculated by the server every time the page is requested.

4. Run and test

dotnet run

Open the address shown in the terminal. Refresh the page several times and observe that the server time changes.

What has been achieved?

  • A complete HTML5 response
  • A page title, heading and paragraphs
  • Dynamic server-side time
  • Explicit text/html content type

The HTML still lives inside a C# string, and only the root route exists.

Next: PART 3 adds Home, Privacy and a visible 404 fallback page.