Topic | Value |
---|---|
Id | ASP002 |
Severity | Warning |
Enabled | True |
Category | AspNetCoreAnalyzers.Routing |
Code | AttributeAnalyzer |
Route parameter name does not match the method parameter name.
[HttpGet(""api/orders/{wrong}"")]
public async Task<IActionResult> GetOrder([FromRoute]int id)
{
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
if (match == null)
{
return this.NotFound();
}
return this.Ok(match);
}
In the above example the method parameter id
has not matching parameter in the route.
Use the code fix to change it to:
[HttpGet(""api/orders/{id}"")]
public async Task<IActionResult> GetOrder([FromRoute]int id)
{
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
if (match == null)
{
return this.NotFound();
}
return this.Ok(match);
}
Configure the severity per project, for more info see MSDN.
#pragma warning disable ASP002 // Route parameter name does not match the method parameter name
Code violating the rule here
#pragma warning restore ASP002 // Route parameter name does not match the method parameter name
Or put this at the top of the file to disable all instances.
#pragma warning disable ASP002 // Route parameter name does not match the method parameter name
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing",
"ASP002:Route parameter name does not match the method parameter name",
Justification = "Reason...")]