Skip to content

Latest commit

 

History

History
78 lines (60 loc) · 2.03 KB

ASP002.md

File metadata and controls

78 lines (60 loc) · 2.03 KB

ASP002

Route parameter name does not match the method parameter name

Topic Value
Id ASP002
Severity Warning
Enabled True
Category AspNetCoreAnalyzers.Routing
Code AttributeAnalyzer

Description

Route parameter name does not match the method parameter name.

Motivation

[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.

How to fix violations

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 severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#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

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing", 
    "ASP002:Route parameter name does not match the method parameter name", 
    Justification = "Reason...")]