Newtonsoft.Json SerializeObject without escape backslashes

If this happens to you while returning the value from a WebApi method, try returning the object itself, instead of serializing the object and returning the json string. WebApi will serialize objects to json in the response by default; if you return a string, it will escape any double quotes it finds.

So instead of:

public string Get()
{
    ExpandoObject foo = new ExpandoObject();
    foo.Bar = "something";
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
    return json;
}

Try:

public ExpandoObject Get()
{
    ExpandoObject foo = new ExpandoObject();
    foo.Bar = "something";
    return foo;
}

Leave a Comment