CreatedAtRoute 找不到与提供的值匹配的路由 [英] CreatedAtRoute can't find route matching supplied values

查看:35
本文介绍了CreatedAtRoute 找不到与提供的值匹配的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 .NET Core 教程时遇到了问题.它是用 .NET Core 2.2 编写的,但我想使用当前版本 3.0.

I am running into a problem with a .NET Core tutorial. It is written in .NET Core 2.2, but I want to use the current release 3.0.

这也是我在我的设置和教程中可以找到的唯一区别.

This is also the only difference I can find in my setup vs the tutorial's.

问题如下:我有一个带有 CreatedAtRoute 调用的 HttpPost 路由,但是找不到它必须的路由.通过 Postman 进行测试时,我总是收到此错误:

The issue is as follows: I have a HttpPost route with a CreatedAtRoute call in it, but that can't find the route it has to. I always get this error when testing through Postman:

System.InvalidOperationException: No route matches the supplied values.
   at Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)
...

但是当检查调试器时,我发现一切正常,除了这一行.所以我要上传的新照片也被添加到 Cloudinary 和数据库中.

But when checking through the debugger I see that everything goes fine, excpet for this line. So the new photo I want to upload is also added in Cloudinary and into the database.

我打的电话是:

return CreatedAtRoute("GetPhoto", new {id = photo.Id}, photoToReturn);

这应该在同一个文件中找到这条路线:

This should find this route, in the same file:

[HttpGet("{id}", Name = "GetPhoto")]
public async Task<IActionResult> GetPhoto(int id)
{

这里有什么我想念的吗?我还没有找到任何有用的答案...

Is there anything I miss here? I haven't found any useful answer yet...

编辑:公共存储库可用,克隆和运行时这,我仍然得到同样的错误...

Edit: public repo available, when cloning and running this, I still get the same error...

编辑 2:与此同时,本教程涵盖了更多主题,我发现了这一点:CreatedAtRoute 调用另一个控制器:

Edit 2: in the meanwhile, the tutorial covered more topics and I have found this: CreatedAtRoute that calls another Controller with:

return CreatedAtRoute("GetUser",new {Controller="Users",id=createdUser.Id}, userToReturn);

工作,当试图在同一个控制器内调用路由时,它失败了,还有这个:

works, when trying to call a route inside the same controller, it fails, also with this one:

return CreatedAtRoute(nameof(GetMessage), new {Controller = "Messages", id = message.Id}, message);

在同一个控制器中调用这个路由:

To call this route in the same controller:

[HttpGet("{id}", Name = "GetMessage")]
public async Task<IActionResult> GetMessage(int userId, int id)

推荐答案

代替[HttpGet("{id}", Name = "GetPhoto")]用:[HttpGet("/{id}", Name = "GetPhoto")]

我遇到了同样的错误,我通过在 id 之前放置/"来解决它.

I was getting same error and i solved it by putting '/' before id.

这篇关于CreatedAtRoute 找不到与提供的值匹配的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆