Taurus. MVC 2.2 Open-source release: WebAPI function enhancement (request cross-origin and Json conversion), webapijson
Background:
1: Some users have provided feedback on cross-origin requests.
2: Some users have feedback about parameter acquisition.
3: JsonHelper enhancement.
In combination with the above conditions, this article was written due to the update of Version 2.2.
Open Source Address:
Https://github.com/cyq1162/taurus.mvc
Next we will introduce the enhanced features:
1: Cross-origin request
In addition to the common JsonP cross-origin, Html5 starts to support enhanced cross-origin, which is more convenient. You only need the Server Request Header output:
1 if (context. Request. UrlReferrer! = Null & context. Request. Url. Host! = Context. request. urlReferrer. host) 2 {3 // cross-origin access 4 context. response. appendHeader ("Access-Control-Allow-Origin", "*"); 5 context. response. appendHeader ("Access-Control-Allow-Credentials", "true"); 6}
Code above:
1: The source host and the requested host can be checked for cross-origin. If yes, the cross-origin identity is returned. 2: The first * indicates to allow any request, of course you can also specify multiple "http://a.com, http:// B .com ". 3: The second "true" indicates that cross-origin cookie operations are allowed. Otherwise, no row is allowed, rather than false.
In addition, an article on the internet says that when cross-origin is used, the browser sends an OPTIONS pre-request, and the framework also handles the issue:
1 if (context.Request.HttpMethod == "OPTIONS")2 {3 context.Response.StatusCode = 204;4 context.Response.AppendHeader("Access-Control-Allow-Method", "GET,POST,PUT,DELETE");5 context.Response.AppendHeader("Access-Control-Allow-Origin", "*");6 context.Response.AppendHeader("Access-Control-Allow-Headers", context.Request.Headers["Access-Control-Allow-Headers"]);7 context.Response.End();8 }
Status Code 204. Compared with 200, it does not need to return the Conent content, but only the request header.
During the Demo, I tested the Get request and found that the browser did not issue a pre-request. The scenario or environment may be different.
The following is a simple test:
In the console (aries.cyqdata.com), initiate a cross-origin request (http: // localhost: 13508/home/index ):
If CORS is disabled:
<add key="IsAllowCORS" value="false"/>
The result is:
Therefore, the Framework supports cross-origin requests by default. If you do not want to support cross-origin requests, you can disable them!
2: obtain common Request Parameters
It is found that some users are still thinking about ASP. NET WebAPI when using Taurus. MVC.
For example:
http://localhost:13508/api/default?id=1
The method is defined as follows:
public void Default(int id){ }
Of course, an exception is thrown, and no method is found. By default, the Framework collects method names without parameters.
As a result, the user crashes and does not know how to get the parameter. I also fail. How can I not know? At least I should know the Request.
The following describes how to obtain parameters encapsulated by the framework: 1: Query <T> ("field") (encapsulated from Request)
For Get or Post parameters such as a = 1 & B = 2, Use Query <T> ("field.
public override void Default() { int id = Query<int>("id"); }
2: Para Parameters
For parameters such as/api/user/3, you can directly take the Action parameter (user) and Para parameter is 3 (separated by: Controller/Action/Para)
public override void Default(){ string para3 = Para;}
3: Get enhanced parameters: GetJson Method 1: In some scenarios, the data of the Peer Post is not in the format of key = value,:
It is a string of Json :( {a: "1", "B": "2"}). At this time, the conventional method cannot get the data.
The native can be obtained through the HttpInputStream stream, but the framework is humanized:
The GetJson () method can be used to obtain:
public override void Default() { Write(GetJson()); }
Execution output result:
2: Abnormal GetJson (): supports converting Get and Post parameters to Json
This method can not only get the Json of direct Post, but also convert a = 1 & B = 2 directly to Json return, for example:
Initiate a request:
Request result:
If the Post request processing result is consistent, no duplicates will be returned!
4: enhanced GetEntity <T> () method 1: for general parameters of Post or Get: a = 1 & B = 2, except for json, you can also convert objects:
Initiate a request:
Request result:
The results for the demo as Get and Post are consistent!
2: GetEntity supports infinitely complex nesting
You can Post complex Json: {a: {B: 2, c: 3}, f: ['1', '2']}, as long as the object corresponds, can be converted.
3: GetList <T> Method
Not yet provided: For Json array conversion of Post [{}, {}], you can use:
List<AB> list = JsonHelper.ToList<AB>(GetJson());
The above is a very practical method for getting parameters to convert Json !!!
For Json output, you can use the Write method to convert complex objects into Json.
Previous section: the http://www.cnblogs.com/cyq1162/p/6069020.html is introduced and does not repeat.
Summary:
Taurus. MVC still follows simple, practical, and efficient principles in processing webapis.
Welcome to Taurus!