C# 从HttpResponseMessage读取字符串
我正在尝试从我的HttpResponseMessage读取字符串。这是我的控制器C# 从HttpResponseMessage读取字符串,c#,string,httpresponsemessage,C#,String,Httpresponsemessage,我正在尝试从我的HttpResponseMessage读取字符串。这是我的控制器 [HttpGet] [Route("api/Predracun/GetPredracunBroj/")] public string GetPredracunBroj() { string broj = ""; List<Predracun> pred = db.Predracun.ToList(); if (pred.Count != 0) { br
[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;
}
[HttpGet]
[路由(“api/Predracun/GetPredracunboj/”)
公共字符串getPredracUnboj()
{
字符串broj=“”;
List pred=db.Predracun.ToList();
如果(pred.Count!=0)
{
broj=db.Predracun.OrderByDescending(x=>x.broj).Select(x=>x.broj.First();
int crtica=broj.IndexOf('/');
字符串brSledeci=broj;
brSledeci=broj.子串(0,crtica);
返回BRSLEECI;
}
其他的
返回broj;
}
其返回的数字为字符串格式。如何读取表单上的字符串
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;
}
}
httpresponsemessageresponsestring=predracanservice.GetActionResponse(“GetPredracunBroj”);
if(responseString.IsSuccessStatusCode)
{
if(responseString.Content.ReadAsAsync().Result!=“”)
{
List brj=responseString.Content.ReadAsAsync().Result;
br=brj[0];
//br=wait responseString.Content.ReadAsStringAsync();//对!
prvibroj=转换为32(br);
++proj;
}
}
。结果您不应使用。这可能导致僵局。还可以从api返回字符串作为响应,因此;您应该获取字符串作为结果,而不是列表。我希望这有帮助
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;
}
公共异步任务getPredracUnboj()
{
int结果=0;
HttpResponseMessageResponseMessage=predracanService.GetActionResponse(“GetPredracunBroj”);
if(responseMessage.IsSuccessStatusCode)
{
string responseString=await responseMessage.Content.ReadAsStringAsync();
如果(!int.TryParse(响应字符串,输出结果))
{
抛出新ArgumentException($“参数不是预期的格式[{responseString}]”);
}
}
返回结果;
}