The correct way to return JSON in the Web API

Source: Internet
Author: User

Source: http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html

When using the Web API, sometimes just want to return JSON, there are many ways to implement this function, this article provides two ways, a traditional, one author thinks is the correct method.

JSON in Web api–the formatter based approach

The most common way to support JSON is to first clear all other formatters, and then keep only jsonmediatypeformatter.

With the Httpconfiguration instance, you will simply clear all the formatters and add the Jsonmediatypeformatter again.

The implementation code is as follows:

Configuration. Formatters.clear (); configuration. Formatters.add (New Jsonmediatypeformatter ());

This way, although it can be implemented, all conent negotiation will still occur, which will incur the following additional overhead. Because, you already know the result to return, and just want to return the JSON, other content negotiation no need.

The following method can be a good solution to this problem.

JSON in Web api–the Conneg based approach

The best approach is to use a custom content negotiation that returns only JSON result instead of the default content negotiation in the Web API.

Conneg is extended by implementing the Icontentnegotiator negotiator method. The Negotiator method returns Contentnegotiationresult (it wraps the headers and formatter of your choice).

The following method gives the custom Conneg negotiator by passing a jsonmediatypeformatter, so that it always returns Applicaton/json of Content-type and Jsonmediatypeformatter. This approach avoids the need to recreate the formatter once per request.

The code is as follows:

public class jsoncontentnegotiator:icontentnegotiator{    private readonly jsonmediatypeformatter _jsonformatter;    Public Jsoncontentnegotiator (Jsonmediatypeformatter formatter)     {        _jsonformatter = formatter;        }    Public contentnegotiationresult Negotiate (type type, httprequestmessage request, Ienumerable<mediatypeformatter > formatters)    {        var result = new Contentnegotiationresult (_jsonformatter, New Mediatypeheadervalue (" Application/json "));        return result;}    }

Next, you need to register your new implementation mechanism on the Httpconfiguration instance:

var jsonformatter = new Jsonmediatypeformatter ();//optional:set Serializer Settings hereconfig. Services.replace (typeof (Icontentnegotiator), New Jsoncontentnegotiator (Jsonformatter));

By replacing the default defaultcontentnegotiator, we use our custom jsoncontentnegotiator, which only supports JSON and can be returned immediately.

If you want to know more about content negotiation, you can check out this article by author.

Summarize

By replacing the default defaultcontentnegotiator of the system with a custom jsoncontentnegotiator, a good implementation of the Web API only returns the functionality of JSON, with no additional overhead.

Original address: http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/

Author's explanation of content negotiation: http://www.strathweb.com/2012/07/ everything-you-want-to-know-about-asp-net-web-api-content-negotation/

The correct way to return JSON in the Web Api (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.