ASP.NET Core 8 Website - Home, Privacy and 404 Routes

PART 3: Creating Home, Privacy and 404 Routes

This part expands the minimal application into a small multi-page website with navigation and a custom fallback response.

1. Route plan

PageRoute
Home/
Privacy/privacy
Not foundAny unmatched route

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>
            <nav>
                <a href="/">basicdotnet</a>
                <a href="/">Home</a>
                <a href="/privacy">Privacy</a>
            </nav>
            <main>
                <h1>Welcome to basicdotnet</h1>
                <p>A simple ASP.NET Core 8 website.</p>
                <p>It's {{serverTime}} on the server.</p>
            </main>
        </body>
        </html>
        """;
    return Results.Content(html, "text/html");
});

app.MapGet("/privacy", () =>
{
    string html = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Privacy - basicdotnet</title>
        </head>
        <body>
            <nav>
                <a href="/">basicdotnet</a>
                <a href="/">Home</a>
                <a href="/privacy">Privacy</a>
            </nav>
            <main>
                <h1>Privacy Policy</h1>
                <p>basicdotnet is a demonstration application.</p>
                <p>This website does not collect or store personal information.</p>
            </main>
        </body>
        </html>
        """;
    return Results.Content(html, "text/html");
});

app.MapFallback(() =>
{
    string html = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Page Not Found - basicdotnet</title>
        </head>
        <body>
            <nav>
                <a href="/">basicdotnet</a>
                <a href="/">Home</a>
                <a href="/privacy">Privacy</a>
            </nav>
            <main>
                <h1>404 - Page Not Found</h1>
                <p>The requested page could not be found.</p>
                <p><a href="/">Return to the Home page</a></p>
            </main>
        </body>
        </html>
        """;
    return Results.Content(html, "text/html",
        statusCode: StatusCodes.Status404NotFound);
});

app.Run();

3. Run and test

dotnet run
  • Open / for Home.
  • Open /privacy for Privacy.
  • Open /contact to test the 404 page.

Without MapFallback, an unknown path still returns status 404, but its response body is empty and may look like a blank page.

Current limitation

The complete HTML structure and navigation markup are repeated three times.

Next: PART 4 adds an external CSS file through wwwroot.