ASP.NET uses AJAX

xiaoxiao2021-03-14  203

Asynchronous JavaScript and XML (Ajax) recently set off the climax to be completely reached in Google in Google Suggest and Google Maps. For ASP.NET, Ajax does not need to return to server-side processing, allowing client (browser) with rich server-side capabilities. In other words, it provides a framework for asynchronous assignment and processing requests with the server response. Ajax took advantage of some of the priorities that were not very novel, but the hobby for these technologies (adding to ajax) suddenly heated.

Please try Michael Schwarz's Ajax .NET wrapper, easy and convenient deployment through its ASP.NET developers, it is easy to use the AJAX functionality page. It should be noted that this wrapper is in the initial development phase, so there is no complete maturity.

However, this technology like Ajax is likely to destroy the hierarchical architecture (N-TIER). My opinion is that AJAX adds the possibility of a logical layer (even worse, business layer) penetrating to the representation layer. As my serious architect, this idea may be aftershoot. I feel that the use of Ajax even has a little over the hierarchy border, which is also worth thinking. Of course, this is dependent on specific projects and environments.

Start

How is it working - overview

AJAX relies on a proxy (Broker assigns and processes the request for the round-trip server. For this, the .NET wrapper relies on the client XMLHttpRequest object. Most browsers support XMLHttpRequest objects, which is why choosing it. Because the purpose of the wrapper is to hide the implementation of XMLHttpRequest, we will no longer discuss it in detail.

The wrapper itself works by marking the .NET function as a Jax method. After tag, Ajax creates a corresponding JavaScript function, which is the same as any JavaScript function) as a proxy, can use XMLHttpRequest calls on the client. These agents reproduce back to the server-side function.

Is it complicated? It is not complicated. Let's take a look at an example. Suppose there is a .NET function:

Public Int Add (int first number {return number;}

Ajax .NET wrapper will automatically create a JavaScript function called "Add" with two parameters. When you call this function using JavaScript (on the client), the request will be passed to the server and return the result to the client.

default setting

Let's first introduce the steps used in the "Install" project. If you know how to add a .dll file reference, you can skip this section.

First, if you haven't, download the latest Ajax version. Unzip the downloaded file and put the Ajax.dll in the reference folder of the project. The "References" node of the organic SOLUTION EXPLORER in Visual Studio.NET and selects Add Reference. In the open dialog box, click Browse and find the REF / AJAX.DLL file. Click Open and OK (confirm) in turn. This allows you to program with the Ajax .NET wrapper.

Establish httphandler

In order to ensure proper operation, the first step is to set the HTTPHANDLER of the wrapper in web.config. No need to explain what httphandlers and how it works, just know that they are used to handle ASP.NET requests. For example, all * .aspx page requests are handled by System.Web.ui.PageHandlerFactory class. Similarly, we let all the requests for Ajax / *. ASHX are processed by ajax.pagehandlerfactory:

... Brief, the above code tells ASP.NET, and any requests that match the specified path (Ajax / *. ASHX) are processed by Ajax.PageHandlerFactory instead of the default handler factory. You don't need to create an Ajax subdirectory, using this mysterious directory just to allow other Httphandlers to use .ashx extensions in the subdirectory established.

Create a page

Now we can start coding. Create a new page or open the existing page, add the following code to the Page_Load event in the file:

Public class index: system.Web.ui.page {private void page_load (object sender, eventargs e) {ajax.utility.registertypeforajax (typeof (index)); // ...} // ...}

Calling RegisterTypeforajax will trigger JavaScript on the page (or manually add the following two lines of code in the page):