Web API does not return JSON

Asked

Viewed 652 times

1

retornomade a web api c# where by default returns xml, tried from various internet methods turn this return to json but when I make a request keeps coming in xml, someone could help?

method giving the result:

 // GET: api/OINV/5
    [ResponseType(typeof(OINV))]
    public IHttpActionResult GetOINV(int id)
    {
        OINV oINV = db.OINV.Find(id);
        if (oINV == null)
        {
            return NotFound();
        }

        return Ok(oINV);
    }

webapiconfig.Cs

 public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Web API routes
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );


    }
  • Where is there any return there?

  • 1

    Put the method that returns the value

  • Hello Leandro, thanks for responding, I will be editing and putting the return

  • Present the method that is returning xml

  • @Leandroangelo altered leandro, for a look.

  • Tried instead of text/html for application/json?

  • already @Barbetta, it even loads in json but in 1 second blink and back to xml, very strange.

  • [Produces("application/json")] before the declaration of your controller class no longer resolves?

  • @Leandroangelo tried here but the error of namespace could not be found

  • Ok... which version are you talking about and which class your controller is inheriting

  • @Leandroangelo web api Entity framework, created the controller with Entity framework, db also

  • controller with Entity framework? sorry I’ve never heard of it

  • This, I right-click on the controller > add>controller folder from which database table to create. It creates crud alone

  • ok.... but the controller does not inherit from the Entity framework, right? and as for the version, I refer to Asp.net

  • Good, I am far from the agr pc but tomorrow I can bring this information

  • Good morning, @Leandroangelo version 4.6.1

Show 11 more comments

3 answers

2


You really have nothing to worry about, ASP.NET Web API 2 with the Framework 4.X will serialize the answers as much as XML, how much JSON as defined in the request. You should not worry about how this content is appearing in the browser because the Web API is not presenting content for a user reading, but rather serving as an interface for communication between applications.

Microsoft documentation

That is by default he is already attending clients who intend to deserialize the content both in XML how much JSON. But, if you want to restrict your application to return only in JSON format you can do as suggested by @Barbetta and remove Mediatype from xml from the formatters list.

Now, if the issue is just for you to view the data in the browser for testing and "documentation" you can implement features that are specialized for this, such as the Swagger.

But what I would really recommend, once you are starting a new application, is to already work with the version of ASP.Net Core that will only bring you benefits (tutorial).

See the example below: Requisição pelo Postman

  • Hello Leandro! Thank you very much for the answer and explanation, really did not know that there would be no problem in the return. Thanks also for the past tips!

1

One option is to remove the SupportedMediaType XML

public static void Register(HttpConfiguration config)
{
    //Outras Configurações
    config.Formatters.XmlFormatter.SupportedMediaTypes
        .Remove(config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"));
}
  • Didn’t work out either, buddy.

0

  • Hello Michael, unfortunately it didn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.