locked
Escaping / Character in Web API Http Get RRS feed

  • Question

  • User-385702356 posted

    Hi,

    Could please explain how to escape / Character in Web API Http Get without using any adhoc methods. The problem comes from the database values which contains some special characters like /. I construct the uri from these data values which results 404 error while invoking

    Thanks in advance,

    Holy

    Thursday, October 25, 2018 8:16 PM

Answers

  • User753101303 posted

    As this is in a db you can't replace / with - to use AUDIO-PHOTO-VIDEO instead ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 3:12 PM

All replies

  • User475983607 posted

    Could please explain how to escape / Character in Web API Http Get without using any adhoc methods. The problem comes from the database values which contains some special characters like /. I construct the uri from these data values which results 404 error while invoking

    You'll need to be a bit more specific and provide example code that reproduces the issue?  

    Thursday, October 25, 2018 8:30 PM
  • User753101303 posted

    Hi,

    You are doing a GET request to another service from the server side? See  https://docs.microsoft.com/en-us/dotnet/api/system.uri.escapedatastring?view=netframework-4.7.2

    By "Adhoc" you mean rolling your own method rather than using a .NET Framework method? You could also build an extension method on HttpClient for example to make this more easily usable.

    Thursday, October 25, 2018 9:31 PM
  • User2103319870 posted

    tains some special characters like /. I construct the uri from these data values which results 404 error

    If you are using Attribute Routing and trying to pass  parameter to api. Consider using Querystring to pass parameters. 

    To escape slash (\) You could encode the values in querystring and then call the api

    To encode values in client side javascript use encodeURIComponent()

    To encode values in C# use UrlEncode

    Friday, October 26, 2018 1:08 AM
  • User2103319870 posted

    tains some special characters like /. I construct the uri from these data values which results 404 error

    If you are using Attribute Routing and trying to pass  parameter to api. Consider using Querystring to pass parameters. 

    To escape slash (\) You could encode the values in querystring and then call the api

    To encode values in client side javascript use encodeURIComponent()

    To encode values in C# use UrlEncode

    Friday, October 26, 2018 1:09 AM
  • User-385702356 posted

    Hi,

    here's  the example of my http get I do in my Web API

    The yellow part is the dynamic part where the value comes from database. 

    this works

    https://localhost:44391/api/categories/ALIMENTARE/families

    and this too works with a space

    https://localhost:44391/api/categories/ACCESSORI%20CASA/families

    whereas the following does n't work (http 404) %2F for foward slash (/)

    https://localhost:44391/api/categories/AUDIO%2FFOTO%2FVIDEO/families

    Thanks,

    Holy

    Friday, October 26, 2018 1:33 PM
  • User475983607 posted

    I assume you are trying to pass "AUDIO/FOTO/VIDEO" as a string.  As suggested  by A2H above above, pass the parameter(s) in the querystring rather than the route.

    Friday, October 26, 2018 1:55 PM
  • User-385702356 posted

    Thanks, Is there a different way out? I design the whole system with the following logic. Replacing with query string parameters costs me a lot :(

    Friday, October 26, 2018 2:00 PM
  • User475983607 posted

    Thanks, Is there a different way out? I design the whole system with the following logic. Replacing with query string parameters costs me a lot :(

    Not that I know of... if it were the last parameter you could use the "catch all" route {*token}.  Being it is in the middle of the URL, I imagine a non URL characters is required or you'll craft a custom route handler. 

    Friday, October 26, 2018 2:57 PM
  • User753101303 posted

    As this is in a db you can't replace / with - to use AUDIO-PHOTO-VIDEO instead ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 3:12 PM