TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rajet Veshin
NA
2
5.9k
Double back slashes inserted on receiving JSON(HttpClient)
May 20 2018 3:46 AM
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);
}
// end try
catch
(Exception ex)
{
return
BadRequest(ex.Message);
}
// end catch
}
// end method
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\""
Reply
Answers (
1
)
Switch to another view if login success in mvc4 angularJS
Mapping of classes and sub classes by fluent NHibernate