简体   繁体   English

如何从 .Net Core 中的 Content.ReadAsStringAsync 方法获取字符串?

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

相关问题 使用Web客户端调用时不包含Content.ReadAsStringAsync()的定义 - does not contain definition for Content.ReadAsStringAsync() when using web client call 如何返回任务 <T> 从HttpResponseMessage.Content.ReadAsStringAsync()。ContinueWith()方法? - How to return the Task<T> from HttpResponseMessage.Content.ReadAsStringAsync().ContinueWith() method? 从 response.Content.ReadAsStringAsync() 恢复对象 - Recover objects from response.Content.ReadAsStringAsync() .Net Core:如何从 URL 字符串中获取 RouteData? - .Net Core : How to get RouteData from URL string? await actionContext.Request.Content.ReadAsStringAsync()返回一个空字符串 - await actionContext.Request.Content.ReadAsStringAsync() returns an empty string 从电子邮件字符串 .net core 获取主机 - Get host from email string .net core 读取请求的内容-ReadAsStringAsync() - reading the content of a request - ReadAsStringAsync() 如何在.net核心WebApi上获取请求字符串 - How to get request string on .net core WebApi 如何从.net核心服务器调用获取.net客户端方法的返回类型 - How to get the return type from a .net core server call to the .net client method 如何在 ASP.NET Core 6 Web API 中以像素形式获取图像内容并从中创建图像? - How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM