Skip to content

Latest commit

 

History

History
78 lines (60 loc) · 2.1 KB

ASP001.md

File metadata and controls

78 lines (60 loc) · 2.1 KB

ASP001

Parameter name does not match the name specified by the route parameter

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

Description

Parameter name does not match the name specified by the route parameter.

Motivation

[HttpGet(""api/orders/{id}"")]
public async Task<IActionResult> GetOrder([FromRoute]intwrong)
{
    var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == wrong);
    if (match == null)
    {
        return this.NotFound();
    }

    return this.Ok(match);
}

In the above example the route parameter id has not matching parameter in the method.

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 ASP001 // Parameter name does not match the name specified by the route parameter
Code violating the rule here
#pragma warning restore ASP001 // Parameter name does not match the name specified by the route parameter

Or put this at the top of the file to disable all instances.

#pragma warning disable ASP001 // Parameter name does not match the name specified by the route parameter

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing", 
    "ASP001:Parameter name does not match the name specified by the route parameter", 
    Justification = "Reason...")]