Problem
How to create a route, regardless of whether the client passes this parameter, can be successfully matched.
Solution Solutions
Both centralized Routing and attribute routing for ASP. NET WEB APIs support routing declaration optional Parameters.
An optional parameter can be specified by routeparameter.optional in a centralized route, and routeparameter.optional is equivalent to urlparameter.option in MVC.
attribute routing, by adding a greeting as a suffix after an optional parameter. At the same time, you must specify a default value for it
Working principle
Fundamentally, the optional parameter routing supported by the ASP. is actually a variant of the default value route. Routeparamter is a mutable type in code snippet 3-8. This example is intended to be a comparison to illustrate that the route optional parameter is a variant of the route default value.
In attribute routing, add a question mark suffix to the route parameter to Routeentry, using the default value of Routeparameter.optional.
At run time, when the framework processes HTTP requests, Httproutingdispatcher removes them if the optional parameters do not have values. He is able to support/resource and/resource/{optionalparameter}.
Code Demo
As shown in code snippet 3-9, the attribute route used defines the route optional parameter, which is the basic paging that allows the client to pass parameters.
The same route, we can use centralized routing as shown in code snippet 3-10 to define route optional parameters.
[Boiled ASP. NET Web API2 methodology] (3-4) to set routing options