locked
Data Service and Special Characters in Service Operation Query RRS feed

  • Question

  • Hello,

    I'm having a problem with one of my WCF Data Services built in .NET 4.0.  The service needs to be able to accept specialy characters (specifically ampersand) when calling a Service Operation however i'm getting a Bad Request - Error in query syntax error. I've read the Allowing special characters as keys in WCF Data Services over ASP.net blod entry and tried to add the following to the web.config but I still get the error.

    <httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0"/>
    <pages validateRequest="false"/>
    

     Here is a sample of the query:

    http://<domain>/<service>.svc/<operation>?namePart='A&B'

    Anybody have any suggestions on how to fix this?

    Monday, January 24, 2011 3:34 PM

Answers

  • Hi William,

    The product team member suggested us to use Uri.EscapeDataString on the service operation parameter to solve the issue. 
    ===========================================================================
                Uri uri = new Uri("http://localhost:27301/WcfDataService1.svc/");
                TestDBEntities svc = new TestDBEntities(uri);
                string u = Uri.EscapeDataString("'A&B'");
                Uri relativeUri = new Uri(uri, "GetPeopleByName?name=" + u);
                var query = svc.Execute<Person>(relativeUri).ToList();
    ===========================================================================

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by William Klein2 Wednesday, January 26, 2011 4:53 PM
    Wednesday, January 26, 2011 3:36 AM
    Moderator
  • You're welcome, William!

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 27, 2011 9:48 AM
    Moderator

All replies

  • Hi William,

    Welcome to WCF Data Services forum!

    It's strange that the "&" character does throw the "Bad Request" error.  However, other characters like "%" works.   I will consult the product team for some ideas and get back to you as soon as I receive any updated messages. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 3:07 PM
    Moderator
  • Hi William,

    The product team member suggested us to use Uri.EscapeDataString on the service operation parameter to solve the issue. 
    ===========================================================================
                Uri uri = new Uri("http://localhost:27301/WcfDataService1.svc/");
                TestDBEntities svc = new TestDBEntities(uri);
                string u = Uri.EscapeDataString("'A&B'");
                Uri relativeUri = new Uri(uri, "GetPeopleByName?name=" + u);
                var query = svc.Execute<Person>(relativeUri).ToList();
    ===========================================================================

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by William Klein2 Wednesday, January 26, 2011 4:53 PM
    Wednesday, January 26, 2011 3:36 AM
    Moderator
  • Hi Michael,

    This did not fix the issue.  All the Uri.EscapeDataString did was replace the ampersand with %26, but the error still occurs.

    Also one other thing, in your example, line 4 the single quotes were missed should have been:

    Uri relativeUri = new Uri(uri, "GetPeopleByName?name='" + u + "'");

    Wednesday, January 26, 2011 3:23 PM
  • Hi William,

    I put the single quotes inside the string u.  Also, I tested the sample codes at my side and they work fine.  Do you still get the same error with my sample codes?  

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 26, 2011 3:42 PM
    Moderator
  • Michael,

    I found my problem. There is currently a service infront of the WCF Data Service that accepts the same URL format but when it appears to unicode the URL before calling the WCF Data Service.

    Thanks for your help.

    Wednesday, January 26, 2011 4:53 PM
  • You're welcome, William!

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 27, 2011 9:48 AM
    Moderator