For the asp.net WebForm project, there are about three Ajax operations: web Service. asmx file), general processing program. ashx) and some Ajax controls.
For. for the moment, the ajax control provided by. net only needs to introduce additional code files to perform operations on Ajax in the other two methods: asmx and ashx, and the web Service also needs to introduce a cs file), If You Want To Example. add some custom Ajax operations to the aspx page, and these Ajax operations will not be used on other pages, so you have to introduce additional code files to complete this operation. If this Ajax operation is very simple, A simple function can be executed. Isn't it a very troublesome process? As a result, as the number of Ajax operations in the project increases, both the ashx and asmx files will increase over time, and the project maintenance will also double. Why can't we put the background code of Ajax operations directly in the Example. aspx. cs file corresponding to Example. aspx? If you can do this, the above two kinds of troubles will be solved, right?
As a result, the following Ajax framework is implemented based on the above ideas. First, let's look at the implementation mechanism of this framework:
Is a part of the Page lifecycle events in the HTTP application pipeline and asp.net Page of The aspx request received by the reduced IIS. Page itself is inherited from the IHttpHandler interface. IHttpHandler provides an important constraint method ProcessRequest, which is used to connect to the received information HttpContext for specific processing, generally, the processing program and web Service also implement their own IHttpHandler and provide some Ajax services. For specific operation procedures, please find MSDN on your own.
The principle is to handle the first event PreInit at the beginning of the page lifecycle. Once the hijacked request is found to be an ajax request, the reflection of C # is used to call aspx. the method defined in cs. After the method is executed, call Response. the End () method directly jumps to the EndRequest event of the pipeline to End the request. In this way, you do not need to take any unnecessary page lifecycle steps to complete an Ajax operation. If a normal operation is found, the normal process is followed.
The following is a simple example to illustrate the use of the Ajax framework:
1. Add a solution
2. Create a new Default. aspx page
3. Create a called test method on the Default. aspx. cs page:
- public List<string> TestAjaxMethod(int a, string b, float c)
- {
- return new List<string> { a.ToString(), b, c.ToString() };
- }
4. Write an Ajax request in Default. aspx
- PowerAjax. AsyncAjax ('testajaxmethod', [1, 2, "333", "sss"], function (SucceessResponse ){
- // Code after successful execution
- });
PowerAjax. js is a simple JS class library encapsulated by Jquery for this framework. This class library has two main methods: PowerAjax. asyncAjax and PowerAjax. syncAjax provides synchronous operations and asynchronous operations, with three parameters:
The first parameter is the name of the Ajax method to be operated in aspx. cs using the name reflection method ).
The second parameter is an array consisting of parameter list data.
The third parameter is the callback method executed after the operation is successful, which is similar to the delegate in c.
The code for this simple JS library is as follows:
- Var PowerAjax = function (){}
- PowerAjax. _ Private = function (){}
- // Perform asynchronous operations
- PowerAjax. AsyncAjax = function (methodName, paramArray, success ){
- PowerAjax. _ Private. Ajax (methodName, paramArray, success, true );
- }
- // Perform synchronization operations.
- PowerAjax. SyncAjax = function (methodName, paramArray, success ){
- PowerAjax. _ Private. Ajax (methodName, paramArray, success, false );
- }
- PowerAjax. _ Private. Ajax = function (methodName, paramArray, success, isAsync ){
- Var data = {};
- Switch (paramArray. length ){
- Case 0:
- Data = {'isjaxrequest': true, 'methodname': MethodName };
- Break;
- Case 1:
- Data = {'isjaxrequest': true, 'methodname': MethodName, "param0": paramArray [0]};
- Break;
- Case 2:
- Data = {'isjaxrequest': true, 'methodname': MethodName, "param0": paramArray [0], "param1": paramArray [1]};
- Break;
- Case 3:
- Data = {'isjaxrequest': true, 'methodname': MethodName, "param0": paramArray [0], "param1": paramArray [1], "param2 ": paramArray [2]};
- Break;
- Case 4:
- Data = {'isjaxrequest': true, 'methodname': MethodName, "param0": paramArray [0], "param1": paramArray [1], "param2 ": paramArray [2], "param3": paramArray [3]};
- Break;
- Case 5:
- Data = {'isjaxrequest': true, 'methodname': MethodName, "param0": paramArray [0], "param1": paramArray [1], "param2 ": paramArray [2], "param3": paramArray [3], "param4": paramArray [4]};
- Break;
- }
- Var url = document. location. href;
- $. Ajax ({
- Type: "post ",
- Url: url,
- Data: data,
- Async: isAsync,
- Datatype: "json ",
- ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
- Success: function (response ){
- Success (response );
- },
- Error: function (response ){
- If (response. status = 500 ){
- Var errorMessage = response. responseText;
- Var errorTitle = errorMessage. substring (errorMessage. indexOf ("<title>") + 7, errorMessage. indexOf ("</title> "))
- Throw new Error ("internal server Error:" + errorTitle );
- }
- }
- });
- }
5. Change the inheritance page of Default. aspx. cs to AjaxBasePage.
- public partial class _Default : AjaxBasePage