Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OData InvalidOperationException. Bad Request - Error in query syntax

OData InvalidOperationException. Bad Request - Error in query syntax

Scheduled Pinned Locked Moved C#
asp-nethelpcsharpdatabasedotnet
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bastien Vandamme
    wrote on last edited by
    #1

    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

    L D 2 Replies Last reply
    0
    • B Bastien Vandamme

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Asking for trouble:

      public class Class

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      L 1 Reply Last reply
      0
      • L Lost User

        Asking for trouble:

        public class Class

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        C# is not case-sensitive.

        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.

        P L 3 Replies Last reply
        0
        • L Lost User

          C# is not case-sensitive.

          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.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          It gets fun if you try and consume it in a language like VB.NET though.

          This space for rent

          L 1 Reply Last reply
          0
          • B Bastien Vandamme

            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

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            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[^]

            B 1 Reply Last reply
            0
            • P Pete OHanlon

              It gets fun if you try and consume it in a language like VB.NET though.

              This space for rent

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • L Lost User

                C# is not case-sensitive.

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Really, since when?

                L 1 Reply Last reply
                0
                • L Lost User

                  Really, since when?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    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.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      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.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You're right, of course :thumbsup:

                      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.

                      1 Reply Last reply
                      0
                      • L Lost User

                        C# is not case-sensitive.

                        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.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        The point was: a meaningless name that looks like a key word. It's "confusing" (for the already confused); as indicated by this thread.

                        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                        1 Reply Last reply
                        0
                        • D dan sh

                          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[^]

                          B Offline
                          B Offline
                          Bastien Vandamme
                          wrote on last edited by
                          #12

                          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.
                          
                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups