I am new to .NET Framework and working on service for it.
service.cs
public dynamic GetList(GetList_Request getList_Request)
{
dynamic result = null;
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = null;
var json = JsonConvert.SerializeObject(getList_Request);
var strContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
string apiUrl = "http://10.216.447.19:5006";
response = httpClient.PostAsync(apiUrl, strContent).Result;
try
{
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsAsync<dynamic>().Result;
}
else
{
throw new Exception("API called failed for POST");
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
controller.cs
public dynamic GetList(GetList_Request getList_Request)
{
try
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
GetList_Request response = _serv.GetList(getList_Request);
if (response == null)
return NotFound();
return Ok(response);
}
catch (Exception ex)
{
throw new HttpResponseException(ControllerContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, logger.Error(ex).ToString()));
}
}
I get an error while parsing the result. Result is 200 ok from service but I get an error in the controller. How can I resolve this? I tried multiple ways but was not successful.
In service.cs result = response.Content.ReadAsAsync<dynamic>().Result
is throwing an exception
Message = "No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'text/html'."`
stackTrace at controller:
at
STAR.WebAPI.Controllers.xController.GetList(GetList_Request getList_Request)
in
C:\Users\1000277196\Project\Controllers\xController.cs:line 59
atlambda_method(Closure , Object , Object[] )
atSystem.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor. <>c__DisplayClass6_1.b__3(Object instance, Object[] methodParameters)
atSystem.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
atSystem.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary'2 arguments, CancellationToken cancellationToken)