The recent upsurge in Asynchronous JavaScript and XML (Ajax) is entirely due to Google's use of Google suggest and Google Maps. For ASP. NET, AJAX can be processed on the server without returning data, so that the client (browser) has rich server capabilities. In other words, it provides a framework for Asynchronously assigning and processing requests and server responses. Ajax uses some existing technologies that are not very novel, but the hobby of these technologies (together with Ajax) has suddenly heated up.
Please try Michael Schwarz's Ajax. Net Package, through which ASP. NET developers can quickly and conveniently deploy pages that are easy to use Ajax functions. It should be noted that this package is in the initial development stage, so it is not completely mature.
However, Ajax technology is likely to break the layered architecture (n-tier ). In my opinion, Ajax adds the possibility of indicating that the logical layer (or even worse, the business layer) can penetrate into the presentation layer. Serious architects like me may be afraid of such ideas. I feel that even if Ajax is used a little beyond the boundaries of layers, this price is worth pondering. Of course, this depends on the specific project and environment.
Start:How it works-Overview
Ajax relies on the broker to assign and process requests to and from the server. The. NET package depends on the XMLHTTPRequest object of the client. Most browsers support XMLHttpRequest objects, which is why they are selected. Because the purpose of the package is to hide the implementation of XMLHttpRequest, we will not discuss it in detail.
The wrapper itself works by marking. NET functions as Ajax methods. After marking, Ajax creates the corresponding JavaScript functions. These functions (like any JavaScript function) can be called on the client using XMLHttpRequest as a proxy. These proxies are then mapped back to the server-side functions.
Complicated? Not complex. Let's look at an example. Suppose there is a. net function:
Public int add (INT firstnumber, int secondnumber)
{
Return firstnumber + secondnumber;
}
The Ajax. Net Package automatically creates a JavaScript function named "add" with two parameters. When you call this function using JavaScript (on the client), the request is sent to the server and the result is returned to the client.
Initial settings
First, we will introduce the. dll steps used in the "Install" project. Skip this section if you know how to add a. dll file reference.
First, if not, download the latest Ajax version. Decompress the downloaded file and place Ajax. dll in the reference folder of the project. In Visual Studio. NET, select the "references" node of the Organic Solution Explorer and add reference ). In the displayed dialog box, Click Browse and find the ref/ajax. dll file. Click Open and OK ). In this way, you can program with the Ajax. Net package.
Create httphandler
To ensure normal operation, the first step is to set the httphandler of the package in Web. config. You don't need to explain in detail what httphandlers is and how it works, as long as you know that they are used to process ASP. NET requests. For example, all *. ASPX page requests are processed by the system. Web. UI. pagehandlerfactory class. Similarly, we make all ajax/*. ashx requests processed by Ajax. pagehandlerfactory:
<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "post, get" Path = "ajax/*. ashx"
Type = "Ajax. pagehandlerfactory, Ajax"/>
</Httphandlers>
...
<System. Web>
</Configuration>
In short, the aboveCodeTells ASP. NET that any request that matches the specified path (ajax/*. ashx) is handled by Ajax. pagehandlerfactory rather than by default.ProgramFactory. You do not need to create an Ajax subdirectory. This mysterious directory is used to allow other httphandlers to use the. ashx extension in their own subdirectories.
Create page
Now we can start encoding. Create a new page or open an existing page. In the code after file, add the following code for the page_load event:
Public class index: system. Web. UI. Page {
Private void page_load (Object sender, eventargs e ){
Ajax. Utility. registertypeforajax (typeof (INDEX ));
//...
}
//...
}
Calling registertypeforajax will trigger the following JavaScript on the page (or manually Add the following two lines of code to the page ):
<Script language = "JavaScript" src = "ajax/common. ashx"> </SCRIPT>
<Script language = "JavaScript"
Src = "ajax/namespace. pageclass, assemblyname. ashx"> </SCRIPT>
The last line indicates:
Namespace. pageclass -- The namespace and class of the current page (usually the value of the inherits attribute in the @ page command)
Assemblyname -- Name of the Assembly to which the current page belongs (usually the project name)
The following is an example of the sample. ASPX page in the ajaxplay project:
<% @ Page inherits = "ajaxplay. sample" codebehind = "sample. aspx. cs"... %>
<HTML>
<Head>
<Script language = "JavaScript" src = "ajax/common. ashx"> </SCRIPT>
<Script language = "JavaScript"
Src = "ajax/ajaxplay. sample, ajaxplay. ashx"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" method = "Post" runat = "server">
...
</Form>
</Body>
</Html>
You can manually navigate to the SRC path in the browser (ViewSource code, Copy and paste the path) to check whether everything is normal. If both paths output meaningless texts, everything would be fine. If nothing is output or an ASP. NET error occurs, it indicates that there are some problems.
Even if you don't know how httphandlers works, the above example is easy to understand. Through web. config, we have ensured that all ajax/*. ashx requests are processed by custom handlers. Obviously, the two script labels here will be processed by the Custom Handler.
Http://www.shpan.com/Detail.asp? Id = 396