Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API 2 - double backslash in string after serialization

I'm dealing with issue of backslashes in the string.

I have method like this

public IHttpActionResult GetResult()
{
    return Ok(@"\");
}

But after JSON serialization I get this result in http response

"\\"

Is there any possibility to disable adding backslash during serialization? I know that I can do it by replacing \\ with \ before response but it is not elegant way for me.

like image 304
Jozef Cechovsky Avatar asked May 03 '16 07:05

Jozef Cechovsky


1 Answers

You can't disable adding a backslash before a \ because it wouldn't be valid JSON (see here). The backaslash will always be added in the following cases:

enter image description here

But once your JSON is deserialized you should only get a single backslash for every \\ in your JSON string.

like image 110
Nasreddine Avatar answered Oct 31 '22 12:10

Nasreddine