liveBook · Manning

Default value

As explained in ASP.NET Core in Action, 3e

A default parameter value is a value specified in a method signature that is supplied when an argument is omitted.

A default value supplies a value when an argument or route segment is omitted. In ASP.NET Core routing, a template such as {name=all} uses all when the URL omits name. section 6.0 section 6.3.2

Default values support two related forms:

  • Route defaults match multiple URL shapes while producing the same route values; /product/shoes and /product/shoes/all both produce category=shoes and name=all. section 6.3.2
  • Handler defaults supply a value when model binding cannot provide an argument, such as int id = 0 in a local endpoint function. section 7.5
  • Keep optional route parameters at the end unless a default value supplies the omitted segment; an optional id cannot appear without category and name. section 6.3.2

Product route with a default

A route template containing /product/{category}/{name=all}/{id?} can match /product/shoes, /product/shoes/formal, and /product/shoes/formal/3. The first URL uses name=all; the last also captures id=3. section 6.3.2

Table 6.1 URLs that would match the template of figure 6.7 and their corresponding route values (view table figure)

URL

Route values

/product/shoes/formal/3

category=shoes, name=formal, id=3

/product/shoes/formal

category=shoes, name=formal

/product/shoes

category=shoes, name=all

/product/bags/satchels

category=bags, name=satchels

/product/phones

category=phones, name=all

/product/computers/laptops/ABC-123

category=computers, name=laptops, id=ABC-123

Compare URLs with and without omitted segments and inspect the route values assigned to each. section 6.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains route matching, assigned route values, parameter placement, and handler signatures, but does not develop broader binding internals.
Examples
Uses product URLs, route-value tables, and a stock handler with int id = 0.
What it assumes
Assumes basic route templates and parameters, while distinguishing optional route segments from defaulted handler parameters.
What you can do after
Apply it via ASP.NET Core route templates and endpoint-handler signatures to accept omitted URL or argument values.
How it connects
Connects default values with routing, optional parameters, model binding, and endpoint handlers.

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 ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.