[英]Read string from HttpResponseMessage
I am trying to read string from my HttpResponseMessage. 我正在尝试从HttpResponseMessage中读取字符串。 This is my controller.
这是我的控制器。
[HttpGet]
[Route("api/Predracun/GetPredracunBroj/")]
public string GetPredracunBroj()
{
string broj = "";
List<Predracun> pred = db.Predracun.ToList();
if (pred.Count != 0)
{
broj = db.Predracun.OrderByDescending(x => x.Broj).Select(x => x.Broj).First();
int crtica = broj.IndexOf('/');
string brSledeci = broj;
brSledeci = broj.Substring(0, crtica);
return brSledeci;
}
else
return broj;
}
Its returning number in string format. 它的返回数字以字符串格式。 How do I read that string on my form?
如何在表单上读取该字符串?
HttpResponseMessage responseString = predracunService.GetActionResponse("GetPredracunBroj");
if (responseString.IsSuccessStatusCode)
{
if (responseString.Content.ReadAsAsync<string>().Result != "")
{
List<string> brj = responseString.Content.ReadAsAsync<List<string>>().Result;
br = brj[0];
// br = await responseString.Content.ReadAsStringAsync(); //right!
prvibroj = Convert.ToInt32(br);
++prvibroj;
}
}
.Result you should not use. 结果不应该使用。 It may couse deadlock.
它可能会导致死锁。 Also you return string as response from api, hence;
同样,您从api返回字符串作为响应。 you should get string as a result not List.
结果应该是字符串而不是List。 I hope this helps.
我希望这有帮助。
public async Task<int> GetPredracunBroj()
{
int result = 0;
HttpResponseMessage responseMessage = predracunService.GetActionResponse("GetPredracunBroj");
if (responseMessage.IsSuccessStatusCode)
{
string responseString = await responseMessage.Content.ReadAsStringAsync();
if (!int.TryParse(responseString, out result))
{
throw new ArgumentException($"Argument is not expected format [{responseString}]");
}
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.