1

I am trying to convert and object "Persona" to a json string in .net Framework 4 and i need help.

I have tried this (using System.Text.Json)

public HttpResponseMessage Get(int id){
  HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    Personas persona = context.tPersonas.FirstOrDefault(p => p.idPersona == id);
    if (persona != null){
      var jsonData = JsonSerializer.Serialize(persona);
      response.Content = new StringContent(jsonData);
      response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
      return response;
    }
  else
    return response = new HttpResponseMessage(HttpStatusCode.BadRequest);            
} 

And this (using Newtonsoft.Json);

 public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            Personas persona = context.tPersonas.FirstOrDefault(p => p.idPersona == id);
            if (persona != null)
            {
                response.Content = new StringContent(JsonConvert.SerializeObject(persona));
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                return response;
            }
            else
                return response = new HttpResponseMessage(HttpStatusCode.BadRequest);            
        }

When debugging, "persona" has data and "Serialize" shows null.

I tried also "Request.CreateResponse" but it is not recognized as a valid code for some weird reason.

Which step am I Skipping?

cgratelli
  • 75
  • 1
  • 11
  • with Newtonsoft it is working. Your problem is not there var persona = new{ Name = "name", LastName = "lastname" }; var st = JsonConvert.SerializeObject(persona); – Zinov Mar 19 '20 at 02:00
  • st variable ends null, don’t know why. Thoughts? – cgratelli Mar 19 '20 at 02:35
  • clean your solution, build and re-run it again, I personally tested it and it works – Zinov Mar 19 '20 at 02:38
  • Thanks sir! It works as you said. Do you have any clue how to send that JSON in case of successful request and an error in case of failed one? – cgratelli Mar 19 '20 at 05:17

1 Answers1

3

If you want to use HttpResponseMessage to return information in asp.core mvc, you need to complete the following steps.

  • Install Microsoft.AspNetCore.Mvc.WebApiCompatShim package for your project.
  • Add services.AddMvc().AddWebApiConventions(); to ConfigureServices in the starup.cs file.

Here is the code:

    public HttpResponseMessage Get(int id)
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        Personas persona = _context.tPersonas.FirstOrDefault(p => p.idPersona == id);
        if (persona != null)
        {
            response.Content = new StringContent(JsonConvert.SerializeObject(persona));
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return response;
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("error message");
            return response;
        }

    } 

You can also refer to this.

Here is the debug process from postman:

enter image description here

LouraQ
  • 6,443
  • 2
  • 6
  • 16