Ajax implemented in ASP.net (1)

Source: Internet
Author: User
Tags add object config functions net reference wrapper visual studio
Ajax|asp.net

The recent upsurge of asynchronous JavaScript and XML (Ajax) is entirely due to Google's use in Google suggest and Google Maps. For ASP.net, Ajax does not require a postback for server-side processing, enabling the client (browser) to have rich server-side capabilities. In other words, it provides a framework for asynchronous assignment and processing requests and server responses.

Ajax has taken advantage of some technologies that are not very novel, but the hobby of these technologies (added together as Ajax) has recently suddenly warmed up.

Please try the Ajax of Michael Schwarz. NET wrapper, which ASP.net developers to quickly and easily deploy AJAX-enabled pages. It should be noted that the wrapper is in the early stages of development and therefore not fully mature.

However, technologies such as Ajax are likely to disrupt layered architecture (n-tier). My view is that Ajax increases the likelihood that the logical layer (or even worse, the business layer) penetrates into the presentation layer. A serious architect like me might be afraid of the idea. I feel that the use of Ajax is worth pondering even if it crosses the level boundaries a little bit. Of course, this depends on the specific project and environment.

Start: how it works--an overview

Ajax relies on proxy (broker) to assign and process requests for a roundtrip server. On this,. NET wrapper relies on client XMLHttpRequest objects. Most browsers support the XMLHttpRequest object, which is why it is selected. Since the purpose of the wrapper is to hide the XMLHttpRequest implementation, we will not discuss it in detail.

The wrapper itself through will. NET functions are marked as Ajax methods to work. Tag, Ajax creates the corresponding JavaScript functions, which, like any JavaScript function, can be invoked on the client using XMLHttpRequest as proxies. These agents are then mapped back to the server-side functions.

Is it complicated? Let's take a look at an example. Suppose there was one. NET function:

public int Add (int firstnumber, int secondnumber)

{

return firstnumber + secondnumber;

}

Ajax. NET wrapper will automatically create a JavaScript function named "Add" with two parameters. When the function is called using JavaScript (on the client), the request is passed to the server and the result is returned to the client.

Initial settings

Let's begin by introducing the steps for the. dll used in the Setup project. If you know how to add a. dll file reference, you can skip this section.

First, if not, download the latest AJAX version. Unzip the downloaded file and place the Ajax.dll in the project's referencing folder. In Visual Studio.NET, organic Solution Explorer's References (Reference) node and select Add Reference (Add Reference). In the Open dialog box, click Browse and locate the Ref/ajax.dll file. Click Open and OK (confirm). So you can use Ajax. NET wrapper has been programmed.

Establish HttpHandler

In order to ensure normal work, the first step is to set the HttpHandler of the wrapper in the web.config. There is no need to explain in detail what httphandlers is and how it works, as long as it is known that they are used to process asp.net requests. For example, all *.aspx page requests are handled by the System.Web.UI.PageHandlerFactory class. Similarly, we allow all requests for ajax/*.ashx to be handled by Ajax.pagehandlerfactory:

<configuration>

<system.web>


<add verb= "Post,get" path= "Ajax/*.ashx"

Type= "ajax.pagehandlerfactory, Ajax"/>


...

<system.web>

</configuration>

In short, the above code tells ASP.net that any request that matches the specified path (AJAX/*.ASHX) is handled by the ajax.pagehandlerfactory rather than by the default handler factory. There is no need to create Ajax subdirectories, this mysterious directory is used only to allow other httphandlers to be able to use it in their own subdirectories. ashx extensions.

Create a page

Now we can start coding. Create a new page or open an existing page, and in the file code, 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 raise the following JavaScript on the page (or add two lines of code to the page manually):

<script language= "javascript" src= "Ajax/common.ashx" ></script>

<script language= "JavaScript"

Src= "Ajax/namespace.pageclass,assemblyname.ashx" ></script>  

The last line of the meaning is:

namespace.pageclass--the namespace and class of the current page (usually the value of the Inherits attribute in the @page Directive)

assemblyname--the name of the assembly to which the current page belongs (usually the project name)

The following is an example of the results of the Sample.aspx page in the Ajaxplay project:

<%@ Page inherits= "ajaxplay.sample" codebehind= "Sample.aspx.cs" ...%>



<script language= "javascript" src= "Ajax/common.ashx" ></script>

<script language= "JavaScript"

Src= "Ajax/ajaxplay.sample,ajaxplay.ashx" ></script>


<body>

<form id= "Form1" method= "POST" runat= "Server" >

...

</form>

</body>

You can manually navigate to the SRC path (view source code, copy and paste path) in the browser to check that everything is OK. If two paths output some (seemingly) meaningless text, it's all right. If there are no outputs or asp.net errors, there are problems in some places.

Even if you don't know how httphandlers works, the example above is easy to understand. Through web.config, we have ensured that all requests for ajax/*.ashx are handled by custom handlers. Obviously, the two script tags here will be handled by a custom handler.

[1] [2] [3] [4] [5] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.