Analyzer: warn when marking a route parameter as optional if it isn't at the end of a route #39486
Labels
analyzer
Indicates an issue which is related to analyzer experience
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
enhancement
This issue represents an ask for new feature or an enhancement to an existing one
feature-routing
Milestone
Is your feature request related to a problem? Please describe.
Route parameters in an ASP.NET Core Web API can be marked as optional with a
?
, e.g.,If a route parameter marked as optional is followed by a non-optional route segment, the optional annotation is ignored. E.g.,
It is not immediately obvious (to me at least) that it would not be possible to have an optional parameter within a route, and there is no run-time error or warning when doing this.
The behavior appears to be the same for nullable value types (e.g.
int?
).Describe the solution you'd like
An analyzer that checks for routes containing optional parameters followed by non-optional route segments, and provides an appropriate warning.
I also couldn't find any mention in the documentation stating that this is not possible.
Additional context
Optional route parameters are allowed to be followed by other optional parameters, so the analyzer should consider this case. E.g.,
Related: #36637
The text was updated successfully, but these errors were encountered: