stijnpiron
stijnpiron

Reputation: 369

CreatedAtRoute can't find route matching supplied values

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.

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)
...

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.

The call I make is:

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...

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)

Upvotes: 2

Views: 1501

Answers (1)

user12771301
user12771301

Reputation: 29

Instead of [HttpGet("{id}", Name = "GetPhoto")] Use: [HttpGet("/{id}", Name = "GetPhoto")]

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

Upvotes: 2

Related Questions