y0j0
y0j0

Reputation: 3592

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.

Upvotes: 5

Views: 5714

Answers (1)

Nasreddine
Nasreddine

Reputation: 37798

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.

Upvotes: 8

Related Questions