aspnetcore: ASP0023 Ambiguous route warning when targeting .NET 8 and using token [action] or [controller] in [Http*] or [Route] attributes

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When targeting .NET 8 preview 6 and using token [action] or [controller] in [Http*] or [Route] attributes on a controller method, an ASP0023 warning is emitted which is a new behaviour that wasn’t observed in .NET 7.

If you ignore the warnings, the code will run and the endpoints can be called and return the expected responses for their respective verbs.

Expected Behavior

No warning generated

Steps To Reproduce

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();

var app = builder.Build();
app.MapControllers();
app.Run();

[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("[action]")]
    public IActionResult GetTime() => Ok(DateTime.Now.ToString("HH:mm:ss"));
    
    [HttpGet("[action]")]
    public IActionResult GetDate() => Ok(DateTime.Now.ToString("yyyy-MM-dd"));
}

Exceptions (if any)

No response

.NET Version

8.0.100-preview.6.23330.14

Anything else?

╰─ dotnet --info                                                                                                                                                                           ─╯
.NET SDK:
 Version:   8.0.100-preview.6.23330.14
 Commit:    ba97796b8f

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-preview.6.23330.14\

.NET workloads installed:
 [wasm-tools]
   Installation Source: SDK 8.0.100-preview.6
   Manifest Version:    8.0.0-preview.6.23329.7/8.0.100-preview.6
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100-preview.6\microsoft.net.workload.mono.toolchain.current\WorkloadManifest.json
   Install Type:              Msi
 [wasm-tools-net6]
   Installation Source: SDK 8.0.100-preview.6
   Manifest Version:    8.0.0-preview.6.23329.7/8.0.100-preview.6
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100-preview.6\microsoft.net.workload.mono.toolchain.net6\WorkloadManifest.json
   Install Type:              Msi

Host:
  Version:      8.0.0-preview.6.23329.7
  Architecture: x64
  Commit:       5340be2ccc

.NET SDKs installed:
  7.0.306 [C:\Program Files\dotnet\sdk]
  8.0.100-preview.6.23330.14 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.6.23329.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.6.23329.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0-preview.6.23329.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 26 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@menees correct route token is [action] not {action}

I get no warnings from this:

[Route("api/[controller]/[action]")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet]
    public IActionResult GetTime() => Ok(DateTime.Now.ToString("HH:mm:ss"));
    
    [HttpGet]
    public IActionResult GetDate() => Ok(DateTime.Now.ToString("yyyy-MM-dd"));
}

This has warnings but they are fixed in 8.0.3 (not released yet):

[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("{action}")]
    public IActionResult GetTime() => Ok(DateTime.Now.ToString("HH:mm:ss"));
    
    [HttpGet("{action}")]
    public IActionResult GetDate() => Ok(DateTime.Now.ToString("yyyy-MM-dd"));
}

I think we can make the analyzer special case {action}.

Using [action] token replacement is much more common, but a {action} parameter works with routing, so the analyzer should handle it without false positives.

Thanks, @jeffraska! That’s good to know, and that’s an interesting subtlety. It wasn’t clear to me from the route template documentation, which lists examples using {action}. However, when I change my example to use [Route("[action]")], Swagger works, each action still works, and ASP0023 no longer appears.

Can anybody confirm, was this released in 8.0.1, or is it not fixed yet? I didn’t find it in release notes.