Skip to content

Getting Started

Mike Christensen edited this page Aug 28, 2026 · 2 revisions

Getting started

This guide creates a minimal ASP.NET Core application whose /hello URL maps to a Hello page class.

Requirements

  • A current .NET SDK and ASP.NET Core application
  • The KitchenPC.Imp package
dotnet new web --name HelloImp
cd HelloImp
dotnet add package KitchenPC.Imp

This installs the latest stable package. Pin an explicit version in applications that require reproducible restores.

Configure the middleware

Replace Program.cs with:

using Imp;

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

app.UseStaticFiles();
app.UseImp(config => config
   .PageAssembly(typeof(Program).Assembly)
   .RootPageNamespace("HelloImp.Pages"));

app.Run();

Imp is terminal middleware: it handles the request and does not invoke middleware registered after it. Put exception handling, authentication, authorization, and static-file handling before UseImp.

Create a page

Add Pages/Hello.cs:

using System.Text.Encodings.Web;
using Imp;

namespace HelloImp.Pages;

public sealed class Hello : BasePage
{
   public string Name { get; set; } = "World";

   public override Task Render(HttpResponse response)
   {
      var name = HtmlEncoder.Default.Encode(Name);
      return response.WriteAsync($"<h1>Hello, {name}!</h1>");
   }
}

Run the application:

dotnet run
  • /hello maps to HelloImp.Pages.Hello.
  • /hello?Name=Mike binds Name and renders Hello, Mike!.
  • / maps to a class named HelloImp.Pages.Default.

Always encode request-derived text before writing HTML. Query binding converts values; it does not perform output encoding or validation.

Add a custom 404 page

public sealed class NotFound : BasePage
{
   public override void PreRender(HttpResponse response) =>
      response.StatusCode = StatusCodes.Status404NotFound;

   public override Task Render(HttpResponse response) =>
      response.WriteAsync("<h1>Page not found</h1>");
}

Register it:

.NotFoundPageType<HelloImp.Pages.NotFound>()

Next steps

Clone this wiki locally