liveBook · Manning

Parameter

As explained in .NET in Action, Second Edition

A query parameter is a key-value input appended to a URL that supplies options or filters for a web request.

A query parameter supplies an option after the path of a request URL. In an ASP.NET Core minimal API, a handler can receive that value without naming it in the route. For example, `/{text}` supplies `text` from the path, while `?font=DotMatrix` supplies `font` from the query string. section 3.1

Listing 3.7 Rendering with custom font from optional query string
using AsciiArtSvc;                           #1
 
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
  
app.MapGet("/", () => "Hello World!");
app.MapGet("/{text}",                        #2
  (string text, string? font) =>             #3
  AsciiArt.Write(text, font));               #4
  
app.Run();

The route contains `{text}` but not `font`. The handler accepts `string? font` and passes it to the rendering method; the nullable type makes the query parameter optional. section 3.1

The parameter name matters: `MapGet` looks for a matching name in the route before using the query string. Use a nullable handler type when a value may be omitted. Changing `string? font` to `string font` makes a request without `font` fail. A value that cannot be converted to the handler’s specified type also makes the request fail. section 3.1

Query parameters can also shape a collection response. An endpoint listing available fonts uses `skip` and `take` for paging, with defaults when either nullable integer is absent. Further optional parameters filter by name or text direction and select a sort order. section 3.1.1

Listing 3.12 Adding skip parameter to all-fonts query
app.MapGet("/",
  (int? skip, int? take) =>       #1
  AsciiArt.AllFonts.Value
    .Skip(skip ?? 0)              #2
    .Take(take ?? 100));          #3

The handler accepts `int? skip` and `int? take`, then applies `Skip(skip ?? 0)` and `Take(take ?? 100)`. The defaults keep requests without either parameter usable. section 3.1.1

How .NET in Action, Second Edition covers it

How deep it goes
Explains name-based binding, nullable optionality, conversion failures, and handler defaults through implementations; does not detail the binding internals.
Examples
Uses an ASCII-art font selector and a font-list endpoint with skip, take, name, direction, and order inputs.
What it assumes
Assumes familiarity with C# handler parameters and URLs while demonstrating how a handler distinguishes path inputs from query inputs.
What you can do after
Build it from scratch in a minimal API: accept optional query inputs and apply them to rendering or collection results.
How it connects
Connects query parameters to route parameters, nullable types, minimal APIs, and LINQ collection operations.

Also explained in

Compare how 31 books explain parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.