ASP.NET Core 2.1 Routing - CreatedAtRoute - 没有路由匹配提供的值

joh*_*ohn 5 .net c# asp.net-core asp.net-core-2.1

我有以下情况

[Route("api/[controller]")]
[ApiController]
public class FooController: ControllerBase
{
    [HttpGet("{id}", Name = "GetFoo")]
    public ActionResult<FooBindModel> Get([FromRoute]Guid id)
    {
        // ...
    }
}

[Route("api/[controller]")]
[ApiController]
public class Foo2Controller: ControllerBase
{

    [HttpPost("/api/Foo2/Create")]
    public ActionResult<GetFooBindModel> Create([FromBody]PostFooBindModel postBindModel)
    {
        //...
        return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);

    }
}
Run Code Online (Sandbox Code Playgroud)

PS:getBindModel是GetFooBindModel类型的实例.我正在接受

InvalidOperationException:没有路由与提供的值匹配.

我也试过换线 GetFooBindModel

return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);
Run Code Online (Sandbox Code Playgroud)

但仍然是同样的错误.

Arm*_*and 7

FooController中的操作方法(Get)的名称与HttpGet Attribute上的路由名称相匹配.您可以在c#中使用nameof关键字:

[HttpGet("{id}", Name = nameof(Get))]
public ActionResult<FooBindModel> Get([FromRoute]Guid id)
{
          ...
}
Run Code Online (Sandbox Code Playgroud)

并而不是硬编码路径名称使用nameof再次:

return CreatedAtRoute(nameof(FooController.Get), new { id = getBindModel.Id }, getBindModel);
Run Code Online (Sandbox Code Playgroud)

然后再试一次;