ASP.NET Web application API tutorial (a) - using data
flow
Believe that there have been many articles to introduce ASP.Net Web API technology,
this article mainly introduces how to use the data flow, HTTPS, and scalable Web API
technology, series there are three main contents.
The main contents are as follows:
I. the data flow
II using HTTPS
III scalable Web API documentation
Project environment requirements
; VS 2012 (SP4) and above,
; The.net framework 4.5.1
; Nuget package, can be in packages. Config file search
This paper involves the knowledge points
1. ActionFilter
2. AuthorizationFilter
3. DelegateHandler
4. The company Web API routing properties
5. MediaTypeFormatter
6. OWIN
7. Self Hosting
8. The Web API documentation and extensible function
The.net framework
1. Async/Await
2. .NET reflection
3. Serialization
4. ASP.NET Web API/MVC Error handling 5. IIS, HTTPS and Certificate 6. Design principles and technology preface
Since the ASP.NET MVC 4.net framework to support ASP.NET Web API, ASP.NET Web API based on HTTP protocol, is an ideal platform to build RESTful services and processing data, aims to use HTTP technology support for multiple platforms.
ASP.NET Web API to request - response message transformation model is given priority to, the client sends a request to the server, the server response to client requests.The response can be synchronous or asynchronous.
Personally think that USES Web apis to create applications need to be aware of three key points:
; By adopting the method of service and meet the goals
; Each method's input, such as request
; The output of each method, such as the response
Typically, Asp.Net Web API definition method syntax one-to-one with HTTP methods, such as custom method name GetPysicians (), and HTTP Get method to match.Below is a common matching table.
But in many cases, this method is not practical, if you want to in a single API controller classes defined in multiple Get or Post method, in this case, the need to define the contains the path of action, the action as part of a URI.The following is the configuration code:
1: public static void Register(HttpConfiguration config)
2: {
3: // Web API configuration and services
4: // Web API routes
5: config.MapHttpAttributeRoutes();
6:
7: config.Routes.MapHttpRoute(name: "PhysicianApi" ,
8: routeTemplate: "{controller}/{action}/{id}" ,
9: defaults: new { id = RouteParameter.Optional });
10: }
But this method is not enough to cope with all situations, if you want to achieve from the central warehouse to Delete files, and want to invoke the same method to Get the file, in this case, the Web API framework need to disguise the Get and Delete the corresponding HTTP method attribute.As shown in figure:
RemoveFile method can be Delete (HttpDelete) or Get (HttpGet) method calls at the same time, to a certain extent, the HTTP method makes developers named API "methods" simple and standard.
Web API framework also provides some other functions to handle the path problems, similar to the path of the MVC processing method.So can define different types of the Action method.
The data flow
The most common web App performs operations is to get the data flow.ASP.NET Web API to deal with the client and server side transmission of massive data flow and data flow can be derived from the catalog file, but also binary files in the database.This paper mainly introduces two methods of "Download" and "Upload" to realize data flow related functions, the Download from the server to Download data operation, while the Upload is uploading data to the server.
Related projects
; WebAPIDataStreaming
; WebAPIClient
; POCOLibrary
Before the interpretation of the code, the first to get to know how to configure the IIS (7.5) and Web API service Web. Config file.
1. Ensure that Downloads/Uploads document is read and write access.
2. Ensure that there is enough capacity or because of the public security space processing large files.
3. If the file is bigger
A. configure Web. Config file, ensure that the response time when the maxRequestLength executionTimeout is reasonable.Specific value mainly depends on the size of the data, allowing one-off the maximum of 2 GB of data upload
B. to ensure maxAllowedContentLength requestFiltering part configuration is set up correctly, the default value is 30 MB, the maximum of 4 gb
Once completed configuration in advance, then create data streaming service is very simple, first of all need to define the file stream "ApiController", as follows:
1: ///
2: /// File streaming API
3: ///
4: [RoutePrefix( "filestreaming" )]
5: [RequestModelValidator]
6: public class StreamFilesController : ApiController
7: {
8: ///
9: /// Get File meta data
10: ///
11: /// FileName value
12: ///
13: [Route( "getfilemetadata" )]
14: public HttpResponseMessage GetFileMetaData( string fileName)
15: {
16: // .........................................
17: // Full code available in the source control
18: // .........................................
19:
20: }
21:
22: ///
23: /// Search file and return its meta data in all download directories
24: ///
25: /// FileName value
26: ///
27: [HttpGet]
28: [Route( "searchfileindownloaddirectory" )]
29: public HttpResponseMessage SearchFileInDownloadDirectory( string fileName)
30: {
31: // .........................................
32: // Full code available in the source control
33: // .........................................
34: }
35:
36: ///
37: /// Asynchronous Download file
38: ///
39: /// FileName value
40: ///
41: [Route( "downloadasync" )]
42: [HttpGet]
43: public async Task
44: {
45: // .........................................
46: // Full code available in the source control
47: // .........................................
48: }
49:
50: ///
51: /// Download file
52: ///
53: /// FileName value