[英]How to get the string from Content.ReadAsStringAsync method in .Net Core?
I am trying to read the response from WEB API, that returns string.我正在尝试读取来自 WEB API 的响应,该响应返回字符串。 I use below statement:
我使用以下语句:
response.Conent.ReadAsStringAsync().Result.ToString();
I was expecting the result to be "TEST" but I am getting ""TEST"".我期待结果是“TEST”,但我得到的是“TEST”。
How do I remove the extra characters from the output.如何从输出中删除多余的字符。
I ran the API alone in Postman, and getting expected result "TEST".我在 Postman 中单独运行 API,并获得了预期的结果“TEST”。 But when I try to read the response via code, I am getting extra characters.
但是当我尝试通过代码读取响应时,我得到了额外的字符。
Not sure, where I am missing.不确定,我在哪里失踪。 Any help on this really appreciated.
对此的任何帮助都非常感谢。
Edit: Adding screenshot of the response编辑:添加响应的屏幕截图
The correct way would be to use await
to read the string result:正确的方法是使用
await
来读取字符串结果:
var result = await response.Conent.ReadAsStringAsync();
And you would not need to use .ToString()
because the result is already a string.而且您不需要使用
.ToString()
因为结果已经是一个字符串。
To manually remove the double quotes from the output you can just trim it:要从输出中手动删除双引号,您只需修剪它:
result = result.Trim('"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.