Key template value 'GetItems(Id={id})' for key segment 'id' does not start with '{' or ends with '}'

Viewed 180

I'm trying to use OData in my project.

I have this class JournalCopy which has ICollection<JournalItemCopy> Items property. I tried to get this Items through JournalCopy and I want to use OData for filtering and expanding the results.

Here is my action:

[ODataRoutePrefix("JournalRepetitives")]
[Authorize(AuthenticationSchemes = TenantRequired.DefaultSchemeName)]
public class JournalRepetitivesController : ODataController
{
    private readonly ReadModelContext _readModelContext;

    public JournalRepetitivesController(ReadModelContext readModelContext)
    {
        _readModelContext = readModelContext;

    }
    private IQueryable<JournalCopy> JournalCopyQuery() => _readModelContext.Set<JournalCopy>();

    [EnableQuery]
    [HttpGet]
    [ODataRoute("GetItems(Id={id})")]
    public IQueryable<JournalItemCopy> GetItems([FromODataUri] string id)
    {
        return JournalCopyQuery().Where(e => e.Id == id)
            .SelectMany(e => e.Items);
    }
}

and I have this configuration:

public static class ReadModelConfiguration
{
    public static ODataModelBuilder Configure()
    {
        var builder = new ODataConventionModelBuilder();

        builder.EntityType<JournalCopy>().HasKey(e => e.Id);
        builder.EntityType<JournalItemCopy>().Collection.Function("GetItems")
            .ReturnsCollectionFromEntitySet<JournalItemCopy>(nameof(JournalItemCopy))
            .Parameter<string>("Id").Required();
    }
}

So when I am using http://localhost:8000/v2/JournalRepetitives/GetItems(Id='76dee73f-8cd4-4311-9708-4701c3d2d017') to call the Api in postman but I got The path template 'JournalRepetitives/GetItems(Id={id})' on the action 'GetItems' in controller 'JournalRepetitives' is not a valid OData path template. Key template value 'GetItems(Id={id})' for key segment 'id' does not start with '{' or ends with '}'. but I don't know its problem.

enter image description here

Any Idea how can I fix it?

1 Answers

I've never used OData routing, but based on the documentation I think it could be a few things:

  1. Remove the quotes in your URL. You usually don't use quotes in URLs.
http://localhost:8000/v2/JournalRepetitives/GetItems(Id=76dee73f-8cd4-4311-9708-4701c3d2d017)
  1. You might need to remove the Id=. So your route should be:
[ODataRoute("GetItems({id})")]

And your URL would look like:

http://localhost:8000/v2/JournalRepetitives/GetItems(76dee73f-8cd4-4311-9708-4701c3d2d017)
  1. I seem to remember an issue with Postman where it would URL encode parentheses even though it doesn't need to, so try disabling URL encoding in Postman. In Postman, under the URL, click on Settings, then turn off "Encode URL automatically".

1

Related