OData InvalidOperationException. Bad Request - Error in query syntax
-
Working with ASP.NET Core and OData v4 I get a
Quote:
InvalidOperationException: The path template 'Classes({key})/Bookings({bookingKey})' on the action 'GetBooking' in controller 'Classes' is not a valid OData path template. Bad Request - Error in query syntax.
I don't see the error in query syntax. Here is the full code of this method in my controller 'Classes'
/// /// Get a specific booking /// /// \[HttpGet\] \[ODataRoute("Classes({key})/Bookings({bookingKey})")\] public async Task GetBooking(\[FromODataUri\] Guid key, \[FromODataUri\] Guid bookingKey) { var @class = await \_context.Classes.FirstOrDefaultAsync(y => y.Id == key); if (@class == null) { return NotFound(); } var booking = \_context.Bookings.Where(y => y.Class.Id == key && y.Id == bookingKey); if (!booking.Any()) { return NotFound(); } return Ok(SingleResult.Create(booking)); }
This method is defined in 'Classes' Controller. I also have a GeBookings method and 2 actions methods defined that cause no issue. When I comment my GetBooking() method I don't have any error. Booking is a [Contained] ICollection of Booking
public class Class { \[Key\] public Guid Id { get; set; } // Others properties \[Contained\] public ICollection Bookings {get; set;} }
Maybe I need to do a pause but I really cannot see my error. Were should I look?
Bastien
-
Working with ASP.NET Core and OData v4 I get a
Quote:
InvalidOperationException: The path template 'Classes({key})/Bookings({bookingKey})' on the action 'GetBooking' in controller 'Classes' is not a valid OData path template. Bad Request - Error in query syntax.
I don't see the error in query syntax. Here is the full code of this method in my controller 'Classes'
/// /// Get a specific booking /// /// \[HttpGet\] \[ODataRoute("Classes({key})/Bookings({bookingKey})")\] public async Task GetBooking(\[FromODataUri\] Guid key, \[FromODataUri\] Guid bookingKey) { var @class = await \_context.Classes.FirstOrDefaultAsync(y => y.Id == key); if (@class == null) { return NotFound(); } var booking = \_context.Bookings.Where(y => y.Class.Id == key && y.Id == bookingKey); if (!booking.Any()) { return NotFound(); } return Ok(SingleResult.Create(booking)); }
This method is defined in 'Classes' Controller. I also have a GeBookings method and 2 actions methods defined that cause no issue. When I comment my GetBooking() method I don't have any error. Booking is a [Contained] ICollection of Booking
public class Class { \[Key\] public Guid Id { get; set; } // Others properties \[Contained\] public ICollection Bookings {get; set;} }
Maybe I need to do a pause but I really cannot see my error. Were should I look?
Bastien
-
Asking for trouble:
public class Class
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
It gets fun if you try and consume it in a language like VB.NET though.
This space for rent
-
Working with ASP.NET Core and OData v4 I get a
Quote:
InvalidOperationException: The path template 'Classes({key})/Bookings({bookingKey})' on the action 'GetBooking' in controller 'Classes' is not a valid OData path template. Bad Request - Error in query syntax.
I don't see the error in query syntax. Here is the full code of this method in my controller 'Classes'
/// /// Get a specific booking /// /// \[HttpGet\] \[ODataRoute("Classes({key})/Bookings({bookingKey})")\] public async Task GetBooking(\[FromODataUri\] Guid key, \[FromODataUri\] Guid bookingKey) { var @class = await \_context.Classes.FirstOrDefaultAsync(y => y.Id == key); if (@class == null) { return NotFound(); } var booking = \_context.Bookings.Where(y => y.Class.Id == key && y.Id == bookingKey); if (!booking.Any()) { return NotFound(); } return Ok(SingleResult.Create(booking)); }
This method is defined in 'Classes' Controller. I also have a GeBookings method and 2 actions methods defined that cause no issue. When I comment my GetBooking() method I don't have any error. Booking is a [Contained] ICollection of Booking
public class Class { \[Key\] public Guid Id { get; set; } // Others properties \[Contained\] public ICollection Bookings {get; set;} }
Maybe I need to do a pause but I really cannot see my error. Were should I look?
Bastien
Looks like you are reusing web api routes and have not enabled custom routing for OData. Can you share the code where you define the routing? Right now, it is assuming Classes to be name of a controller which of course is not the case.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]
-
It gets fun if you try and consume it in a language like VB.NET though.
This space for rent
Pete O'Hanlon wrote:
It gets fun if you try and consume it in a language like VB.NET though.
Not really, there's a special syntax to use those.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
-
I meant that C# nicely distinguishes between a classname with uppercase and lowercase. "Class" is not treated as "class". Not sure if that is sensitive of insensitive.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
I meant that C# nicely distinguishes between a classname with uppercase and lowercase. "Class" is not treated as "class". Not sure if that is sensitive of insensitive.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Yes, C# is case sensitive. That is it is sensitive to the difference between class and Class. If it was insensitive then it would be happy with cLAss, CLAss, clAsS, or any variation thereof, and take them all to mean the same as
class
. -
-
Looks like you are reusing web api routes and have not enabled custom routing for OData. Can you share the code where you define the routing? Right now, it is assuming Classes to be name of a controller which of course is not the case.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]
All my other routes are working fine. GelAll is working, Post is working. I don't get it. Controller name is Classes. Only route that include the key of Booking are not working. What is the logic behind this?
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Oyg.Domain.DataTypes;
using System;
using System.Linq;
using System.Threading.Tasks;namespace Oyg.Api.Controllers
{
public partial class ClassesController : ODataController
{
/// /// Get all bookings. Get the class reservation queue.
///
///
[HttpGet]
[ODataRoute("Classes({classId})/Bookings")]
public async Task GetBookings([FromODataUri] Guid classId)
{
return Ok(await _context.Bookings
.Where(y => y.Class.Id == classId)
.ToListAsync());
}/// /// Get a specific booking /// /// \[HttpGet\] \[ODataRoute("Classes({classId})/Bookings({bookingId})")\] public async Task GetBooking(\[FromODataUri\] Guid classId, \[FromODataUri\] Guid bookingId) { var @class = await \_context.Classes.FirstOrDefaultAsync(y => y.Id == classId); if (@class == null) { return NotFound(); } var booking = \_context.Bookings.Where(y => y.Class.Id == classId && y.Id == bookingId); if (!booking.Any()) { return NotFound(); } return Ok(SingleResult.Create(booking)); } /// /// Add a booking reservation in a class /// /// /// \[HttpPost\] \[ODataRoute("Classes({classKey})/Bookings")\] public async Task PostBooking(\[FromODataUri\] Guid classKey, \[FromBody\] Booking booking) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var @class = \_context.Classes .FindAsync(classKey); if (@class == null) { return NotFound(); } booking.Id = Guid.NewGuid(); booking.Position = \_context.