ASP.NET Core Interview Question #107

What are optional route parameters and default route values?

Routing, Controllers & Model Binding Mid-Level Intermediate

Quick Interview Answer

An optional route parameter is marked with a question mark, such as {id?}. The route can match whether or not that segment is present.

Detailed Explanation

An optional route parameter is marked with a question mark, such as {id?}. The route can match whether or not that segment is present.

A default route value supplies a value when the URL does not contain one. In the common MVC default route, controller defaults to Home and action defaults to Index.

Optional parameters should be used carefully so routes remain understandable and do not create ambiguous endpoint matches.

Code Example

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");