return type, formatter, filter for Web API methods

Source: Internet
Author: User

First, the return type of the action method

A) There are four return types for the method of operation: void, simple or complex type, httpresponsemessage type, Ihttpactionresult type.

b) If the return type is void, the HTTP status code is 204 (No Content) After the request ends, and if the return type is a complex type, the object is converted to a JSON string after the transfer.

c) from the approximate interactive process of the Web API shown, Controll returns the Httpresponsemessage type, which is then converted to HttpResponse to be provided to the caller. You can also manually set a httpresponsemessage in your code and set the HTTP status.

D) Ihttpactionresult

WebAPI 2 can return Ihttpactionresult objects, similar to MVC's actionresult. The classes that implement Ihttpactionresult are:

Returning these types is, you don't have to specify the HTTP status code yourself like Httpresponsemessage.

Second, Web API request\response data format

A) Media type (aka MIME type) specifies the format of the data, such as Type/subtype, text/html, Text/xml, Application/json, Image/jpeg, and so on. In an HTTP request, the method of specifying a MIME type in the request is to use the Accept and Content-type properties in the header. The Accept Header property specifies the format of the response data that the customer expects, and the Content-type header property specifies the format of the data in the request body so that the receiver can parse it into the appropriate format.

Third, media format device

A) the role of the media formatter is to serialize the request or response data according to the use of the Accept and Content-type properties, so that the Web API can understand the data formats.

WebAPI has some media type formatters built in:

Using GlobalConfiguration.Configuration.Formatters, you can get Mediatypeformattercollection, which contains a collection of all these formatters. You can also add\remove the formatter.

b) Jsonmediatypeformatter internal use of third-party open Source Library json.net to perform serialization. The schema writes the JSON property name according to Pascalcase, and if you want to use the CamelCase format, you can configure it in Webapiconfig:

In addition, adding bsonmediatypeformatter to the formatter collection can support the formatting of Bson (binary JSON).

Iv. Media Filter

A) similar to the filters in MVC, provides the ability to provide AOP for logging, exception handling, performance measurement, identity authentication, authorization, and more. Filters implement the System.Web.Http.Filters.IFilter interface, and the important interfaces and classes that can be used to create filters are:

b) If you are creating a logging class, you can use the action method filter, which implements Iactionfilter or inherits the ActionFilterAttribute class in two ways.

? If you implement Iactionfilter, because you want to create an attribute like Logattribute, you also need to inherit the attribute class. then implement Iactionfilter. Executeactionfilterasync method. Trace.wrieline Dictation writes to the Output window.

If you inherit ActionFilterAttribute, you can override the OnActionExecuting, onactionexecuted method, and log the logs.

Learning materials: www.yuanjiaocheng.net (Ape Tutorial) Qingwengang

return type, formatter, filter for Web API methods

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.