I am new to programming and specifically new to .Net MVC(using .net core 2). I am having a controller method as below
- 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);
- }
Here in "responseContent", i am getting extra slashes in my JSON
But in the API method, i am getting properly formatted JSON string My API method is
- [HttpGet] [Route("api/Country/GetCountryList")]
- public IActionResult GetCountryList()
- {
- ProcedureReturnType mediaType = GetMediaType();
- try
- {
- CountryHelper countryHelper = new CountryHelper();
- var result = countryHelper.GetCountries(mediaType);
- return Ok(result);
- }
-
- catch (Exception ex)
- {
- return BadRequest(ex.Message);
- }
- }
Any way, i can solve this issue as i need the list of countries which i expect to get from the valid JSON in listCountries
For example if i write
var result = "\r"; return Ok(result);
Then the result sent is "\r", but on the receiving end at line
responseContent is having the value "\"\r\""