.net core CreatedAtRoute可以';找不到与提供的值匹配的路由
我遇到了一个关于.NET核心教程的问题。它是用.NETCore2.2编写的,但我想使用当前版本3.0 这也是我在设置和教程中能找到的唯一区别 问题如下: 我有一个带有CreatedAtRoute调用的HttpPost路由,但它找不到必须的路由。 通过邮递员进行测试时,我总是会遇到以下错误:.net core CreatedAtRoute可以';找不到与提供的值匹配的路由,.net-core,.net-core-3.0,asp.net-core-3.0,.net Core,.net Core 3.0,Asp.net Core 3.0,我遇到了一个关于.NET核心教程的问题。它是用.NETCore2.2编写的,但我想使用当前版本3.0 这也是我在设置和教程中能找到的唯一区别 问题如下: 我有一个带有CreatedAtRoute调用的HttpPost路由,但它找不到必须的路由。 通过邮递员进行测试时,我总是会遇到以下错误: System.InvalidOperationException: No route matches the supplied values. at Microsoft.AspNetCore.Mvc.C
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和数据库中
我的电话是:
return CreatedAtRoute("GetPhoto", new {id = photo.Id}, photoToReturn);
应在同一文件中找到此路由:
[HttpGet("{id}", Name = "GetPhoto")]
public async Task<IActionResult> GetPhoto(int id)
{
工作时,当尝试调用同一控制器内的路由时,它会失败,也会失败:
return CreatedAtRoute(nameof(GetMessage), new {Controller = "Messages", id = message.Id}, message);
要在同一控制器中调用此路由,请执行以下操作:
[HttpGet("{id}", Name = "GetMessage")]
public async Task<IActionResult> GetMessage(int userId, int id)
[HttpGet(“{id}”,Name=“GetMessage”)]
公共异步任务GetMessage(int userId,int id)
而不是
[HttpGet(“{id}”,Name=“GetPhoto”)]
使用:
[HttpGet(“/{id}”,Name=“GetPhoto”)]
我遇到了同样的错误,我通过在id前面加“/”来解决它。您使用的是
UseRouting
和UseEndpoints
?如果是这样,在3.0版本中对UseEndpoints
的调用是什么样子的?是的,我是。目前是:app.UseEndpoints(endpoints=>{endpoints.mapDefaultControllerOute();})
但它也不能用于endpoints.MapController()
(我以前在其他地方找到的解决方案)您是否同时使用MapDefaultControllerRoute
和MapController
进行了尝试?是的,相同的结果我试图重现,但对我来说效果很好,没有问题。
[HttpGet("{id}", Name = "GetMessage")]
public async Task<IActionResult> GetMessage(int userId, int id)