简体   繁体   English

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

[英]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: getBindModelGetFooBindModel类型的一个实例。 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.

相关问题 ASP.NET Core CreatedAtRoute没有路由匹配提供的值 - ASP.NET Core CreatedAtRoute No route matches the supplied values ASP.NET CORE、Web API:没有路由与提供的值匹配 - ASP.NET CORE, Web API: No route matches the supplied values 当操作名称以“Async”结尾时,Asp.Net Core 3.0 CreatedAtAction 返回“没有路由与提供的值匹配” - Asp.Net Core 3.0 CreatedAtAction returns "no route matches the supplied values" when Action name ends with "Async" CreatedAtRoute 生成一个异常:“没有路由与提供的值匹配。” - CreatedAtRoute generates an exception : “No route matches the supplied values.” CreatedAtRoute:System.InvalidOperationException:没有路由与提供的值匹配 - CreatedAtRoute: System.InvalidOperationException: No route matches the supplied values ASP.NET Core 2.1中的重定向路由 - Redirect routing in ASP.NET Core 2.1 ASP.NET Core“CreatedAtRoute”失败 - ASP.NET Core “CreatedAtRoute” Failure API ASP.NET 5“POST”成功,但出现错误“没有路由与提供的值匹配” - API ASP.NET 5 "POST" successfully, although with the error "No route matches the supplied values" CreateAtAction在ASP.Net Web API中返回“无路由匹配提供的值” - CreateAtAction returns “No route matches the supplied values” in ASP.Net Web API ASP.NET Core 路由属性路由 - ASP.NET Core route attribute routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM