locked
Web Action APi 2 returns an empty json RRS feed

  • Question

  • User-673983033 posted

    I have the following action that is returning an IHttpActionResult, this is the code

    [HttpGet]
    public IHttpActionResult ConsultaPuertos([FromUri] string id, [FromBody] List<PuertosGP> lstPuertosGP, [FromUri] DateTime fecha)
    {
    
    var resultList1 = puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha);//<--- here I get almost 1 data
    return Ok(resultList1); //<---- does not show data ??
    });

    the result is ...

    [
        {}
    ]

    My webApiConfig.cs is...

        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                
                // Web API configuration and services
                config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));//text/html
    .......

    Wednesday, November 20, 2019 3:43 PM

Answers

  • User-673983033 posted

    It was Simple the solution

    add

      using Newtonsoft.Json; to the class that need to be serialized

    and decorate every atribute with:

    [JsonProperty("Field namexxxx")]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 22, 2019 7:40 PM

All replies

  • User475983607 posted

    Return the actual type as openly shown in your similar threads.  Also I strongly recommend that you stop use the "var" keyword until you have a bit more experience.  Use the actual type.

    This method probably always returns a type like IQueryable which is a LINQ query that has not executed or an empty List<T>.   The community cannot see this code as you did not share these bits but this is a very common mistake on the forum.

    var resultList1 = puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha);

    Lastly, please use the Visual studio debugger to view the contents of resultsList1.

    Wednesday, November 20, 2019 4:20 PM
  • User-474980206 posted

     return OK(string),

    returns a  status with specified string as the status text (no body)

       200 <the string value instead of the default "OK">

    if the caller wants the text associated with the status you use calling library's method to get the status test. in XMLHttpRequest is xhr.statusText 

    Wednesday, November 20, 2019 6:27 PM
  • User-673983033 posted

    hi mgebhard

    you're right!! the resultList1 is of tipo List<Myobject> I'm agree with try to do not use var

    Wednesday, November 20, 2019 11:57 PM
  • User61956409 posted

    Hi santival,

    var resultList1 = puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha);

    To troubleshoot the issue, please debug your code and check if the list returned by puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha) method does contain item(s).

    With Regards,

    Fei Han

    Thursday, November 21, 2019 1:49 AM
  • User-673983033 posted

    Hi santival,

    santival

    var resultList1 = puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha);

    To troubleshoot the issue, please debug your code and check if the list returned by puertosGPRepository.ConsultaPuertos(id, lstPuertosGP, fecha) method does contain item(s). 

    YEs it does contain item

    https://imgur.com/a/mmmKwVj

    Thursday, November 21, 2019 2:08 AM
  • User61956409 posted

    Hi santival,

    YEs it does contain item

    https://imgur.com/a/mmmKwVj

    Can not reproduce same issue with my project and testing data, please share the screenshoot of your actual data that your ConsultaPuertos method returned.

    With Regards,

    Fei Han

    Friday, November 22, 2019 8:50 AM
  • User-673983033 posted

    It was Simple the solution

    add

      using Newtonsoft.Json; to the class that need to be serialized

    and decorate every atribute with:

    [JsonProperty("Field namexxxx")]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 22, 2019 7:40 PM