我有以下情况
[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);
}
}
PS:getBindModel
是类型GetFooBindModel
的一个实例。我越来越
InvalidOperationException: No route matches the supplied values.
我也试过改线
return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);
到
return CreatedAtRoute("api/Foo/GetFoo", new { id = getBindModel.Id }, getBindModel);
还是一样的错误
最佳答案
将 FooController 中的操作方法 (Get) 的名称与 HttpGet 属性上的路由名称相匹配。你可以在 c# 中使用 nameof 关键字:
[HttpGet("{id}", Name = nameof(Get))]
public ActionResult<FooBindModel> Get([FromRoute]Guid id)
{
...
}
并且再次使用 nameof 而不是硬编码路由名称:
return CreatedAtRoute(nameof(FooController.Get), new { id = getBindModel.Id }, getBindModel);
再试一次;
关于c# - ASP.NET Core 2.1 路由 - CreatedAtRoute - 没有路由与提供的值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52063794/