1

I'm trying to do something very simple.

 string img = "sadadsasdsa";
 string res = @"<image src=""data:img/jpg;base64," + img + @"""/>";

I get the output as <image src=\"data:img/jpg;base64,sadadsasdsa\"/>

While the output that I want is <image src="data:img/jpg;base64,sadadsasdsa"/>
I don't want the unexpected '\' to be added before data and at the end.

I even tried using the backslash as the escape sequence as
string res = "<image src=\"data:img/jpg;base64," + img + "\"/>";

I still get the same output. What am I missing here?

Okay, when I print the above 'res' string using console.writeline, I'm not getting the magical slash. But when I view it in debugger, I do see it. And I still see it when I return the string from a web API values controller. I guess this has to do with parsing of the string literal

 [HttpGet]
    [Route("api/quotes/image")]
    public string ImageString()
    {
        string imgg = "sadadsasdsa" + "\"";
        string ress = @"<image src=""data:img/jpg;base64," + imgg + "/>";
        return ress;
       }  

When I try to print the string using Console.WriteLine, I don't see the unwanted backslash. But I do see it in the debugger and when returning the string from the WebAPI controller as above.

  • 1
    Please provide [MCVE]. String constants don't magically get slashes in them. – Alexei Levenkov Jan 30 '16 at 06:44
  • @AlexeiLevenkov Okay, when I print the above 'res' string using console.writeline, I'm not getting the magical slash. But when I view it in debugger, I do see it. And I still see it when I return the string from a web API values controller. I guess this has to do with parsing of the string literal – Nithish Inpursuit Ofhappiness Jan 30 '16 at 06:54
  • You really need to provide code that reproduces the issue... For debugger part - expected behavior, there are plenty explanation like http://stackoverflow.com/questions/16196987/c-sharp-how-to-replace-slash-quote – Alexei Levenkov Jan 30 '16 at 06:58
  • @HenkHolterman Right, but when I return this from a WebAPI Controller, in the json I still see the slash – Nithish Inpursuit Ofhappiness Jan 30 '16 at 07:19
  • Yes, also in the debugger. – Nithish Inpursuit Ofhappiness Jan 30 '16 at 07:24
  • 1
    "in the json" - can you show exact result (preferably copy-paste from raw view of Fiddler capture of the call)? It still sounds like you are looking at strings in something that encodes them (as VS immediate/debugger windows) – Alexei Levenkov Jan 30 '16 at 07:34
  • Voting to close as Typo / Cannot Reproduce due to deciphering various output in different tools (Fiddler) and various browsers with escape sequences. As mentioned by OP in comments under accepted answer. – Drew Jan 30 '16 at 18:13

2 Answers2

1

This is a standard debugger behaviour. You will get this:

debugger

while debugging, and this:

console

on the console.

See: Can the Visual Studio Debugger display strings unquoted/unescaped? for more info.

romanoza
  • 4,775
  • 3
  • 27
  • 44
0

The string 'res' seems mangled. However, is it an option for you to use string formatting? E.g.:

string img = "sadadsasdsa";       
string actual = string.Format(
                       CultureInfo.InvariantCulture,
                       "<image src=\"data:img/jpg;base64,{0}\"/>",
                       img);

string expected = "<image src=\"data:img/jpg;base64,sadadsasdsa\"/>";

EDIT Here is a code snippet that returns the concatenated string from you initial question:

public class ValuesController : ApiController 
{
    public IEnumerable<string> Get()
    {
        // your string concat
        string img = "sadadsasdsa";
        string actual = string.Format(
                               CultureInfo.InvariantCulture,
                               "<image src=\"data:img/jpg;base64,{0}\"/>",
                               img);

        // DEBUG ONLY - for simplicity use ASCII encoding
        Encoding enc = Encoding.ASCII;
        foreach (byte b in enc.GetBytes(actual))
        {
            // write bytes and characters
            Debug.WriteLine(b.ToString("X2") + " " + enc.GetString(new byte[] {b}));
        }

        return new string[] { actual };
    }

In the debug window you will see that the string 'actual' does not contain backslashes (0x5C), as desired: 3C < 69 i 6D m 61 a 67 g 65 e 20 73 s 72 r 63 c 3D = 22 " 64 d 61 a 74 t 61 a 3A : 69 i 6D m ...

The HTTP response received contains escaped double-quotes, introduced by a JSON serializer. Since double quotes are meaningful in JSON, they will have to be escaped in JSON values. This can be observed in the Fiddler Response Raw view: Content-Type: application/json; charset=utf-8 ["<image src=\"data:img/jpg;base64,sadadsasdsa\"/>"]

After deserialization the response contains the string without backslashes. This can be observed in the Fiddler Response JSON view. (i use Fiddler 4.6.2.0)

Largely related: question 9462425

You can also look at this using Postman (Chrome extension), which is useful for changing the HTTP request 'Accept' header. 'application/json' will return you JSON, and 'application/xml' will return you XML. Both formats have quite different looks when serialized. Toying the Accept header and looking at the resulting responses illustrates the point about the need to escape meaningful characters.

If this helps, would you be so kind to remove the downvote?

Community
  • 1
  • 1
knoflook
  • 42
  • 2
  • I didn't down vote this, but your code although doesn't work. I'm still getting the unwanted slash – Nithish Inpursuit Ofhappiness Jan 30 '16 at 07:08
  • What do you get exactly? – knoflook Jan 30 '16 at 07:12
  • "" this is what I still get I don't want the backslash inside the src before data – Nithish Inpursuit Ofhappiness Jan 30 '16 at 07:18
  • You see the backslash in some tool that escapes the double quote, I guess. The backslashed are not present in the string 'actual'. – knoflook Jan 30 '16 at 07:20
  • [Question 1928909](http://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal) is about string formatting. WebApi will not mangle your response. Perhaps a formatter kicks in that inserts a backslash? Therefore i have two questions: 1) in which tool do you see the backslash? and 2) what is the value of your Accept header? – knoflook Jan 30 '16 at 07:31
  • 1
    In fiddler, if I see the raw response, I see the slash. And if I go to the JSON tab, I no longer see the slash. In chrome, I don't see the slash...but in IE, the JSON gets downloaded and I open it in notepad, wherein I do see the slash. So, I guess this just has to do with the viewing formatter. I'm going to accept your answer, but if you have anything further to add, you are most welcome! – Nithish Inpursuit Ofhappiness Jan 30 '16 at 11:49
  • Fiddler Raw view shows what travels over HTTP. Since double quotes are meaningful in JSON, your double quotes travel escaped 'over the wire' . Fiddle JSON view deserializes the raw data and is able to display the string 'that you meant to send'. I'll edit my response by including a small experiment. – knoflook Feb 01 '16 at 15:23