我是编程新手,特别是.Net MVC的新手(使用.net core 2)。我有一个控制器方法如下
public async Task<JsonResult> GetCountriesList(string searchText)
{
HttpResponseMessage responseMessage;
string responseContent = string.Empty;
List<Country> listCountries = null;
using (var client = getHttpClient(WebClientContentType.JSON))
{
responseMessage = await client.GetAsync(new Uri(string.Format("{0}{1}", _webApiUrl, "/api/Country/GetCountryList")));
if (responseMessage.IsSuccessStatusCode)
{
responseContent = responseMessage.Content.ReadAsStringAsync().Result;
listCountries =(List<Country>)JsonConvert.DeserializeObject(responseContent, typeof(List<Country>));
if (!string.IsNullOrEmpty(searchText))
{
listCountries = listCountries.Where(m => m.ShortName.ToLower().StartsWith(searchText.ToLower())).ToList();
}
}
else
{
return Json(responseMessage.StatusCode);
}
}
return Json(listCountries);
}
在“responseContent”中,我在JSON中获得了额外的斜杠
但是在API方法中,我得到了正确格式化的JSON字符串 我的API方法是
[HttpGet]
[Route("api/Country/GetCountryList")]
public IActionResult GetCountryList()
{
ProcedureReturnType mediaType = GetMediaType();
try
{
CountryHelper countryHelper = new CountryHelper();
var result = countryHelper.GetCountries(mediaType);
return Ok(result);
} // end try
catch (Exception ex)
{
return BadRequest(ex.Message);
} // end catch
} // end method
任何方式,我可以解决这个问题,因为我需要我希望从listCountries中的有效JSON获得的国家列表
例如,如果我写
var result = "\r";
return Ok(result);
然后发送的结果是“\ r”,但是在
行的接收端responseContent的值为“\”\ r \“”