[英]Get the exact string return by the Ok() in Asp.NET Core 6
I have RESTful API that returns some large JSON files.我有返回一些大型 JSON 文件的 RESTful API。 I want to save the content to a file before sending it to the client.
我想在将内容发送给客户端之前将其保存到文件中。 Here's my endpoint now:
这是我现在的端点:
[HttpPost]
[Route("rest/result")]
public IActionResult GetResult(string requestId)
{
var item = _service.GetItem(requestId);
return item?.Result == null ? NotFound() : Ok(item.Result);
}
I tried this code:我试过这段代码:
[HttpPost]
[Route("rest/result")]
public async Task<IActionResult> GetResult(string requestId)
{
var item = _service.GetItem(requestId);
var str = JsonSerializer.Serialize(item.Result);
await File.WriteAllTextAsync($"c:\\output\\{requestId}.json", str);
return item?.Result == null ? NotFound() : Ok(item.Result);
}
But the file is a bit different.但是文件有点不同。 Probably in terms of encoding because I'm seeing things such as:
"Data":"{'FnId':'10000149'
in the file.可能在编码方面,因为我在文件中看到诸如:
"Data":"{'FnId':'10000149'
之类的东西。
That shouldn't be any problem, as both representations are equivalent.这不应该是任何问题,因为两种表示是等价的。
'
is Unicode for apostrophe ('). '
u0027 是撇号 (') 的 Unicode。 So, special characters are returned in Unicode but will show up properly when rendered on the page:因此,特殊字符在 Unicode 中返回,但在页面上呈现时会正确显示:
"Data":"{'FnId':'10000149'}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.