简体   繁体   English

通过 Asp.NET Core 6 中的 Ok() 获取确切的字符串返回

[英]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.

相关问题 ASP.NET 核心任务<iactionresult>没有异步就无法返回 NotFound() 和 OK()</iactionresult> - ASP.NET Core Task<IActionResult> cannot return NotFound() and OK() without async ASP.net Core MVC 测试/使控制器返回一个字符串? - ASP.net Core MVC testing / make controller return a string? ASP.NET 核心 - 想要使用 Results.Ok 强制文本/纯文本(<string> )</string> - ASP.NET Core - want to force text/plain with Results.Ok(<string>) 如何让 Asp.Net Core 返回正确的视图 - How to get Asp.Net Core to return the correct view 如何测试我的 ASP.NET Core 2.2 Web API GET IActionResult 返回 Ok(object)? - How to test my ASP.NET Core 2.2 Web API GET IActionResult that returns Ok(object)? 从ASP.NET Web API ASP.NET Core 2返回HTML并获取http状态406 - Return HTML from ASP.NET Web API ASP.NET Core 2 and get http status 406 ASP.NET Core 从显式文化中获取资源字符串 - ASP.NET Core get Resource string from explicit culture 在ASP.NET Core DI容器中注入一条记录是否可以? - Is it ok to inject a record in the ASP.NET Core DI container? asp.net 核心试图 select 包含确切 id 的对象 - asp.net core trying to select objects that contains exact id 在asp.net核心api中返回集合 - return collection in asp.net core api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM