Home > database >  Double slash in return value from controller
Double slash in return value from controller

Time:01-14

I have the following controller:

public IActionResult Get()
{
    var res = GetResult();
    // res is a JSON {"hello": "how\are\you"}
    return Ok(res);
}

I'm getting in the result:

{"hello": "how\\are\\you"}

How to remove the double slash from the reusult? Why they're being added?

CodePudding user response:

Why they're being added?

Because that's how you print a backslash in JSON. \ is a special character. It pairs with the next character(s) to define what it prints. For example, \n means to add a new line, not printing "\n". \\ means a print a single backslash.

The server (your code) and the client using your application will understand this escape sequence just fine.

CodePudding user response:

You don't need to remove them since you have them in json already. If you want to get rid of them fix json of the action

var res = GetResult();
res.Replace("\\"," ");
 return Ok(res);
  •  Tags:  
  • Related