[英]ASP.NET Core 2.1 Routing - CreatedAtRoute - No route matches the supplied values
I have the following situation我有以下情况
[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
is an instance of type GetFooBindModel
. PS: getBindModel
是GetFooBindModel
类型的一个实例。 And I am getting我越来越
InvalidOperationException: No route matches the supplied values. InvalidOperationException:没有路由与提供的值匹配。
I also tried changing the line我也试过换线
return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);
to到
return CreatedAtRoute("api/Foo/GetFoo", new { id = getBindModel.Id }, getBindModel);
but still the same error.但仍然是同样的错误。
Match the name of your action method (Get) in the FooController with the route name on HttpGet Attribute.将FooController中的操作方法 (Get) 的名称与 HttpGet 属性上的路由名称相匹配。 You can use the nameof keyword in c#:你可以在 c# 中使用 nameof 关键字:
[HttpGet("{id}", Name = nameof(Get))]
public ActionResult<FooBindModel> Get([FromRoute]Guid id)
{
...
}
and also Instead of hard coding route name use nameof again:并且再次使用nameof而不是硬编码路由名称:
return CreatedAtRoute(nameof(FooController.Get), new { id = getBindModel.Id }, getBindModel);
and Try again;然后再试一次;
The following code works for me:以下代码对我有用:
return CreatedAtRoute(
"GetFoo",
new { controller = "Foo", id = getBindModel.Id},
getBindModel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.