The next entry is my most important part of the WEBAPI, in the current popular internet scene, Webapi can and HTML5, single page application spa and other technologies and concepts are very good combination. The core concept of the so-called ASP. WebAPI is to build a restful Web service that treats a data as a resource, whether it is a service request or a data operation, which differs greatly from the previous soap and XML-RPC architectural styles. That said, many readers may have thought that there was a restful service in WCF, why would they need this webapi? This is true, but the type of service in WCF is more complex because the composition of its communication pipeline is naturally bloated due to the integration of many different communication protocols.
Simply put, WEBAPI is simple and efficient, "You deserve to have"! Let's copy the example of Mr. Jiang to have a preliminary understanding of it, the backend code is as follows:
View Code
The front-end code is as follows:
View Code
As a supplement, Mr. Jiang is using the Knockoutjs as part of the MVVM-style front-end framework. About this piece, there is a problem that bothered me for a long time, that is Knockoutjs and Angularjs who's applicability is stronger, in fact they are not comparable, KNOCKOUTJS only provide part of the work. The following link is an explanation of the problem and concludes that I will learn and use ANGULARJS.
Http://blog.darkthread.net/post-2014-06-07-go-to-angularjs.aspx
Speaking of which, I also thought of the study of a puzzle, so many of the IOC framework in the end which is relatively better? Conclusion is AUTOFAC, it is used in Orchard Open source CMS system, by a mouth,nopcommerce . NET open source e-commerce system is also good oh. For more information on the comparison of the previous IOC frameworks, please see the link below, and the best explanation from Mr. Li:
Http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html
Next, we introduce the server pipeline for ASP. Webapi, which is similar to the ASP. NET MVC pipeline, but there are some differences, but personally it feels like the pipe of the Java EE. As a lot of content is similar, will be a simple introduction, but the framework of asynchronous programming model with a lot of, it is worth learning reference. A simple description of the framework's processing of requests:
The framework provides Httpcontrollerhandler objects through a singleton, multiple Httpwebroute shared objects, and it creates the ASP. NET Web API processing pipeline to the right, activating the pipeline operation by calling the BeginProcessRequest method. The pipeline is actually a httpmessgaehandler chain, and Httpserver and Httpcontrollerdispatcher can be seen as two special Httpmessagehandler, The following is a simple introduction to the related types in tabular form:
type |
Introduction | /tr>
httpmessagehandler |
Core class, processing implementation for requests in SendAsync, processing of responses by return type task |
httprequestmessage |
Content Property encapsulates HTTP principal information |
httpresponsemess The Age |
StatusCode, Reasonphrase property represents the response status code and description |
delegatinghandler |
is used to build the processing chain through the Inn The Erhanlder attribute is passed, is the implementation of the responsibility chain mode? The |
httpserver |
Dispatcher property points to the final dispatcher object, which contains all the configuration information. |
httpconfiguration |
dependencyresolverfilters:authorizationfilter, Actionfilter, Ex Ceptionfilterformatters: Returns the formatter list Includeerrordetailpolicy: Client exception display policy propertiesservices: Returns ServiceContainer, A simple IOCR container, the default implementation of Defaultservices, is very common. |
Httpcontrollerhandler creates the httpserver in a lazy-load manner, and the properties of the dictionary pass related data in the form of key "Ms_httpcontext", "Ms_httproutedata". Httpcontrollerdispatcher is responsible for the final processing of the request, including the activation of the Apicontroller and the execution of the target action, using the following table to describe the process:
Behavior |
Brief introduction |
Activation of the Httpcontroller |
With Httpcontrollerdescriptor, you complete Httpcontroller type resolution, selection, creation, etc. The activation of an IOC-based Httpcontroller can be implemented by customizing the Dependencyresolver or Httpcontrolleractivator. |
Implementation of the Httpcontroller |
Through the Executeasync method, the parameter is httpcontrollercontext, note that link in Urlhelper represents the absolute address, the route relative address |
Choice of Action |
Httpactiondescriptor's Executeasync method implements the action, which supports different HTTP methods in 7, which by default is post. The Httpactionselector component implements the selection of the target action, the return value of the method getactionmapping is a ilookup<string, the httpactiondescriptor> type |
Parsing of model meta-data |
Basically consistent with MVC |
Action parameter Binding |
With Httpparameterdescriptor, httpactionbinding, the binding is done through the executebindingasync of the Httpparameterbinding object, the specific implementation class is: Cancellationtokenparameterbinding errorparameterbinding fomatterparameterbinding: Message body, Html,json,xml Httprequestparameterbinding:httprequestmessage modelbinderparameterbinding: query string, routing data |
Validation of model |
including Dataannotationmodelvalidator Requiredmembermodelvalidator Validatableobjectadapter Errormodelvalidator and other validators, It is important to note that the validation process in this framework is recursive and somewhat different from MVC. |
Action execution and response to the result |
Activates the action through the Httpactioninvoker Invokeractionasync method, converting the return value of the action to Httpresponsemessage by Actionresultconverter, Converters include: Responsemessageresultconverter valueresultconverter<t> voidresultconverter 3 built-in filter filters that work like in MVC |
Fill in the IOC-implemented code and httpparameterbinding flowchart:
View Code
Httpparameterbinding Flowchart:
Finally, we introduce the content related to the WEBAPI client invocation, the first reaction is to make Ajax calls through JavaScript in the Web page, get the data and render it, the consumer of the service is the front page, which is just one of the main ways to call. Another is the invocation via HttpClient, which is similar to a Web service invocation, and the consumer of the service is a generic application. The HttpClient class inherits the abstract class Httpmessageinvoker, the core method SendAsync includes the Httprequestmessage parameter and the Httpresponsemessage return type, As with the previous server-side Httpmessagehandler type, the httpclient is actually a wrapper for that class. The httpcompletionoption is used to set flags for response completion, including reading the message header and reading through the message body. The BaseAddress property is used to specify the WEBAPI base address, Defaultrequestheader is used to add any header, maxresponsecontentbuffersize indicates the size of the read buffer, default 2G, Timeout indicates the time-out period, default 100s. Getasync, Getbytearrayasync, Getstreamasync, Getstringasync are used for http-get requests, and other methods have similar definitions. Following through a server-side self-boarding, the client general invocation of the example to complete the learning, need to be aware of the addition of NuGet Selfhost and the client library, the code is as follows:
View Code
In addition, the WEBAPI Study Series catalogue is as follows, welcome your reading!
Quick Start Series--webapi--01 Basics
Quick Start Series--webapi--02 Advanced
Quick Start Series--webapi--03 frame you deserve it
Quick Start Series--webapi--04 adjustment under old version MVC4
Note: This article is mainly for their own study, the wrong place hope forgive me.
Resources:
[1] Jing Jinnan. The ASP. NET MVC4 framework revealed [M]. Shanghai: Electronic industry Press, 2012. 445-526
Quick Start Series--webapi--03 frame you deserve it