New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ASP.Net Core: internal routing fails with CreatedAtRoute #18
Comments
Coincidentally, I discovered this problem yesterday. I've captured the description and cause of the problem in Issue #19. The issue is specific the route constraint used for URL segment API versioning. This problem only occurs when you use that style of versioning. I've already fixed the issue and it will go out in an official release as soon as possible. |
It might also be worth mentioning that you don't have to re-specify route parameters that are already provided by the current route. For example, the following will work just fine (after the fix): CreatedAtRoute( "GetDocument", new { guid = newguid }, newDoc ); The controller and version route parameters are already provided so you don't need to specify them again. There's nothing wrong with your approach, but you can make things more succinct if you want. :) |
Thank you for your fast reply and analysis!
Thank you, i had that on my first try but it was just another desperate attempt to make it work ;) |
I'm having this problem on CreatedAtAction("Get", new { cId, aId, pId}, result); I still get the error However when I add the version it does work CreatedAtAction("Get", new { version = HttpContext.GetRequestedApiVersion().ToString(), cId, aId, pId}, result); |
You must be versioning by URL segment. CreatedAtAction requires all of the route parameters. Since the API version is special, the ASP.NET doesn't know where it comes from. The ApiVersion type now supports model binding so you don't have to use the extension method anymore, unless you want to. For example: public IActionResult Get(int cId, int aId, int pId, ApiVersion version) => Ok();
public IActionResult Post([FromBody] Result result, ApiVersion version)
{
var cId = result.cId;
var aId = result.aId;
var pId = result.pId;
var apiVersion = version.ToString();
return CreatedAtAction(nameof(Get), new {cId, aId, pId, apiVersion}, result);
} Ultimately, this is another consequence of versioning by URL segment. While it might seem strange, it's completely valid for POST to be v1 and GET to be V2 or vice versa. I hope that helps clear things up. |
This is a bit disappointing. Nah, I'd rather leave it as an empty string. My client app will know where to get it from. |
@DavidRogersDev what is disappointing? Including the necessary route parameters? ASP.NET Core will only consider a very few, well-known intrinsic ambient values such as It's also worth mentioning that you should prefer CreatedAtAction over CreatedAtRoute. The route name specified via CreatedAtRoute must be unique and is not API version aware. The simplest away to get around that is to use a name such as "GetV1", "GetV2", etc, but that gets messy over time. I've yet to find a flexible solution without changing some of the standard routing setup. Not changing routing has always been a standing design principle. CreatedAtAction doesn't have this problem and doesn't require a name. Unless you are trying to build URLs across controllers, that would be the recommended way to go. |
No route matches the supplied values. Can't get why my post-method doesn't work. I think I have the same issue as the people above..
|
@SouhaibAkrouchi the issue is that you've crossed the streams. In |
Aight, thx man! You're the best. |
app.MapControllerRoute( apply this program.cs |
I detected a problem with the routing, if you want to create an object via post and then return a CreatedAtRoute result. It always returns an error 500: "No route matches the supplied values"
If i remove the package (aspnet-api-versioning), and rename the route of the controller to /api/v1 in a fixed way, it works.
The provided samples in this repository are not (unfortunately ) helping in this case, because there are or only 'GET' request samples.
Can you help me? If you need more informations, i would be happy to send you some more.
Here's my code:
The text was updated successfully, but these errors were encountered: