0

I am using ASP.NET Core 5.0 Web api project. On top of this project I have installed Microsoft.AspNetCore.OData 8.0.10 nuget package to enable OData support.

This is how my routes are currently setup:

[ApiController]
    [Route("api/[controller]")]
    public class CustomerController : ODataController
    {
        private readonly oDataContext _context;
       

        public CustomerController(ILogger<CustomerController> logger, oDataContext context)
        {
            _logger = logger;
            _context = context;
        }

        [HttpGet]
        [EnableQuery(AllowedFunctions = AllowedFunctions.AllFunctions & ~AllowedFunctions.All)]
        public IQueryable<Customer> Get()
        {
            return _context.Customers;
        }

       
        [HttpGet("({key})")]
        [EnableQuery(AllowedFunctions = AllowedFunctions.AllFunctions & ~AllowedFunctions.All)]
        //[EnableQuery]
        public async Task<ActionResult<Customer>> GetCustomer(int key)
        {
            var Customer = await _context.Customers
                       .FirstOrDefaultAsync(x => x.CustomerId == key);

            if (Customer == null)
            {
                return NotFound();
            }

            return Customer;
        }
}

With this I am able to work with following routes:

https://localhost:5000/v1/customer$top=10
https://localhost:5000/v1/customer$expand=orders
https://localhost:5000/v1/customer(1)

But when I hit https://localhost:5000/v1/customer(1)/orders I get 404 error. I understand I need to update the route to handle this.

I was reading this article which talks about how to add attribute route.

But If I try to add route attribute like [ODataRoute("Customer({id})/orders", "odata")] I get error. Am I missing any nuget package? How can I add support for $expand when searching by id?

OpenStack
  • 5,048
  • 9
  • 34
  • 69
  • Add[ODataRoutePrefix("v1")] on your controller and [ODataRoute("Customer({id})] on your action acrooding to the document? Ican't use the attribute as you showed even if i installed the package – Ruikai Feng May 19 '22 at 10:02
  • @RuikaiFeng: these attributes `[ODataRoutePrefix("v1")]`, `[ODataRoute("Customer({id})] ` are available via `Microsoft.AspNetCore.OData 8.0.10` nuget package ? or we need another package ? – OpenStack May 19 '22 at 12:18
  • @OpenStack, the attributes no longer exist in 8.0.10 as I believe they've moved towards enabling attribute routing in its place ie. `[HttpGet("routeTemplate")]`. The Odata prefix and odata route attributes shouldn't really be needed anymore. – developer Jun 21 '22 at 04:35
  • Assuming you have your app configuration similar to `opt.Select().Expand().Filter().OrderBy().SetMaxTop(250).Count().SkipToken() .AddRouteComponents(edmModel);`, replacing `[Route("api/[controller")]` with `[ODataRouteComponent]` may get you going. – developer Jun 21 '22 at 04:37
  • Here's un update to that article you referenced: [Attribute Routing 8.0 RC](https://devblogs.microsoft.com/odata/attribute-routing-in-asp-net-core-odata-8-0-rc/) – developer Jun 21 '22 at 04:43

0 Answers0