AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Possible error in route parameter name "authorId"

Open stansp2004 opened this issue 7 years ago • 10 comments

I didn't understand why the example in "Custom model binder sample" section employed "authorId" route template parameter whereas action method's signature awaits parameter named "author":

[HttpGet("get/{authorId}")]
public IActionResult Get(Author author)
{
    if (author == null)
    {
        return NotFound();
    }
    
    return Ok(author);
}

I downloaded source files from the link provided in the abstract, ran them with the Aid of Visual Studio 2017 v.15.9 and noticed that this endpoint didn't work as the bindingContext.ModelName was equal to "". When I renamed the route variable from "authorId" to "author" the endpoint started to work. This is how action method looks after renaming:

[HttpGet("get/{author}")]
public IActionResult Get(Author author)
{
    return Ok(author);
}

Could you possibly update this article if there is an error with route parameter naming?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

stansp2004 avatar Jan 11 '19 20:01 stansp2004

@dotnet-bot Same issue to @stansp2004 when I use WebAPI on Ver2.1 .

makunhappy avatar Feb 07 '19 19:02 makunhappy

I think to make the authorId param work according to its intend ("...by fetching the entity from a data source using Entity Framework Core and an authorId..."), you need to assign a "default authorId" in the AuthorEntityBinder class by adding something like below, after the modelName = bindingContext.ModelName assignment:

if (String.IsNullOrEmpty(modelName)) { modelName = "authorId"; }

With this default name in place, the {authorId} in route binds correctly.

khtang avatar Mar 15 '19 01:03 khtang

Moved to #16319

Rick-Anderson avatar Dec 26 '19 17:12 Rick-Anderson

@Rick-Anderson Would it be ok to ask @pranavkm to have a quick look at this? I've looked into it and can see a couple of ways to fix it, but both seem smelly so it'd be great if one of the experts could look at it. I can't see it taking very long...

serpent5 avatar Jan 02 '20 22:01 serpent5

@pranavkm please review.

Rick-Anderson avatar Jan 02 '20 22:01 Rick-Anderson

I believe the default guidance should be to name the router parameter as the model action parameter - author in this case. @khtang 's suggestion would also work, but it is not really straight forward and it's a bit arbitrary - as in that case you're completely customizing the matching rule.

@pranavkm can you please confirm that the router parameter in the example here should indeed be author instead of authorId?

mkArtakMSFT avatar Jun 28 '20 03:06 mkArtakMSFT

@pranavkm can you please confirm that the router parameter in the example here should indeed be author instead of authorId?

Rick-Anderson avatar Aug 28 '20 02:08 Rick-Anderson

I just ran into something similar after playing around a bit with .NET 5 and I think that the entire example of the custom model binder is quite confusing. The example relies heavily on bindingContext.ModelName but if you are trying to parse the actual request body, this is of no value if I understand correctly.

Although changing the parameter in the route to author instead of authorId might solve the issue, it is far from the convention you should use when writing clean api routes imho. I know this is not supposed to be a real life example but still..

I think it might be better to add an additional example on how to actually parse data from a request body, which is something I was trying to do but turned out not to be as trivial as it thought it would be.

Let me know if this makes any sense. Willing to help improve the documentation based on your feedback.

rwlnd avatar Sep 24 '20 19:09 rwlnd

@brunolins16, Can you review this and see if we need to fix the docs?

rafikiassumani-msft avatar Oct 10 '22 23:10 rafikiassumani-msft

I had a long discussion about this doc, specifically the polymorphic binder sample, and indeed the docs are confusing and not clear about binding from body.

https://github.com/dotnet/AspNetCore.Docs/issues/26626

Related to usage of author or authorId the first example looks wrong should be author, in the route, while the second example explicit talks about a scenario where we want to keep the parameter name and a different route parameter name.

I can send an PR with a quick fix while we decided to improve the doc here.

brunolins16 avatar Oct 11 '22 04:10 brunolins16