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?

4
  • with Newtonsoft it is working. Your problem is not there var persona = new{ Name = "name", LastName = "lastname" }; var st = JsonConvert.SerializeObject(persona); Commented Mar 19, 2020 at 2:00
  • st variable ends null, don’t know why. Thoughts? Commented Mar 19, 2020 at 2:35
  • clean your solution, build and re-run it again, I personally tested it and it works Commented Mar 19, 2020 at 2: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? Commented Mar 19, 2020 at 5:17

1 Answer 1

3

If you want to use HttpResponseMessage to return information in ASP.NET 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 startup.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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much sir! this is what I was looking for :)
I use Minimal API, suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.