liveBook · Manning

Default value

As explained in Pro ASP.NET Core 7, Tenth Edition

A default value is a value assigned to a route segment variable when the corresponding URL segment is absent.

A route default supplies a value for a segment variable when the URL omits that segment. Write it inside the route pattern with an equals sign: `capital/{country=France}`. This lets the route match both `/capital` and `/capital/uk`. section 13.2.2

Check the matched URLs

With `app.MapGet("capital/{country=France}", Capital.Endpoint)`, `/capital` sets `country` to `France`; `/capital/uk` sets it to `uk`. `/capital/europe/italy` still has too many segments to match. Restart ASP.NET Core and request `http://localhost:5000/capital` to check the fallback. section 13.2.2

Don’t confuse a route default with the value model binding supplies when it cannot find request data. An action parameter of type `long` can receive `0` even without a matching route value, query string, or form value. If that becomes a database key, handle the missing match rather than assuming the query will succeed. section 28.3.2

Table 13.6 Matching URLs (view table figure)

URL Path

Description

/

No match—too few segments

/city

No match—first segment isn’t capital

/capital

Matches, country variable is France

/capital/uk

Matches, country variable is uk

/capital/europe/italy

No match—too many segments

The URL table shows exactly which paths the defaulted route matches—and which it does not. section 13.2.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines the equals-sign syntax and tests matching URLs; does not explain routing internals.
Examples
Tests `/capital` for `France`, `/capital/uk` for `uk`, and an overlong path for no match.
What it assumes
Assumes familiarity with ASP.NET Core route patterns and URLs, but spells out how to declare a segment default.
What you can do after
Operate/configure an ASP.NET Core route to accept an omitted segment while assigning its variable a specified value.
How it connects
Distinguishes route defaults from model binding’s fallback argument values and C# parameter defaults.

Also explained in

Compare how 20 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.