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?