AV0023: Route components are ignored
| Value | |
|---|---|
| Rule ID | AV0023 |
| Category | Usage |
| Fix is | Breaking |
Cause
OData route components are added to the options that versioned OData replaces.
Rule Description
Versioned OData resolves the options for the API version of the current request. The options configured for OData itself are not part of that resolution. Route components added without saying which API version they belong to are left behind when the options are resolved, and a prefix stated in both places collides once they are.
Consider the following code:
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddControllers().AddOData(
options => options.AddRouteComponents( "api", model ) );
builder.Services.AddApiVersioning().AddOData();
The route components are added to ODataOptions rather than to the versioned options, so they are never applied.
How to Fix Violations
Add the route components through the options given to the versioned AddOData(), which applies them per API version.
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddControllers().AddOData();
builder.Services.AddApiVersioning().AddOData(
options => options.AddRouteComponents( "api" ) );
See OData options for how route components are applied per API version.
When to Suppress Warnings
It is never safe to suppress this rule. The route components are either ignored or collide with the versioned ones.