1

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
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor. <>c__DisplayClass6_1.b__3(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary'2 arguments, CancellationToken cancellationToken)

halfer
  • 19,824
  • 17
  • 99
  • 186
chink
  • 1,505
  • 3
  • 28
  • 70
  • In your controller you're declaring the type of `response` to be `GetList_Request` which looks suspicious to me. I suggest changing the type to `dynamic` initially, then performing any conversion or serialisation explicitly. Beyond that, it would help to provide the full controller class - the stack track says 'line 59' but which line is that? And what's the `Ok()` method? – Neil T Jun 18 '21 at 07:00
  • `result = response.Content.ReadAsAsync().Result` is throwing an exception `Message = "No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'text/html'."` – chink Jun 18 '21 at 11:33
  • `ReadAsAsync()` is generally used when the expected result is a serialised object such as (or analogous to) JSON. In this case it looks like the response is HTML. Try replacing that line with `result = response.Content.ReadAsStringAsync();` and inspect the result. It's likely that what you'll see isn't something that can be deserialised. (Depending on the result, and what you want to do with it, you might also need to change the method's return type to `string`.) – Neil T Jun 18 '21 at 16:47

1 Answers1

-1

The error is that you are trying to parse result into a dynamic but dynamic is not a type. To sort the problem you need to figure out what type is the api returning back and declare it and use it. To be able to do this, I would use postman, send the request with postman, and see the return type and model it in your application, then parse it.

you have an example of how to parse it in Read HttpContent in WebApi controller

download postman: https://www.postman.com/downloads/

Iria
  • 433
  • 1
  • 8
  • 20