Ashx + jquery, a lightweight Asp.net Ajax Solution

Source: Internet
Author: User

I studied and discussed with shotdog the Ajax solution in Asp.net apart from the huge official Asp.net Ajax solution. The idea is to output data in different ways on the server side, and then use jquery Ajax on the page to call several solutions on the server side:

use a general webform, call it with jquery Ajax on the page, and then obtain the content in from the obtained HTML data, and write the content into the DOM
advantages: you can use existing pages without changing the existing Asp.net development mode. The content obtained by Ajax is HTML text and can be directly written into the Dom.
disadvantage: A waste of content, none of the content except is necessary, and if masterpage is used, it is...
General webform, but response. write () controls the output HTML, and CALLS jquery Ajax on the page to write the obtained content to the Dom.
advantages: the content is clean and not wasted. The content obtained by Ajax is HTML text, writing Dom directly
disadvantages: HTML text needs to be constructed in string form on the server side, which is inconvenient to program and not easy to debug and maintain
using a common webform and response. write () controls the output of JSON data. It is called using jquery Ajax on the page. JSON data is processed as HTML on the client and written into Dom.
advantage: Only JSON data is exchanged, which is extremely clean, in line with the efficient web design concept
disadvantages: JSON data needs to be processed on the client, causing Dom intrusion
using asmx, encapsulated into a web service, use jquery ajax to call the content of asmx and process JSON or XML data on the client into HTML and write it into Dom.
advantage: Only J is exchanged. Son or/XML data, very clean; web service is easy to cross-platform
disadvantage: JSON data needs to be processed on the client and Dom intrusion
use the custom control ascx, then we use a dedicated webform page for wrapper (packaging). On the page, we use jquery ajax to call wrapper webform and write HTML data to the Dom.
advantage: webform is only used as wrapper, custom controls can be dynamically used in Wrapper Based on Different Request Parameters. custom controls output HTML text and can be directly written to Dom. Programming is convenient, supported by vs2008 Code and easy debugging and maintenance
disadvantages: different from the traditional webform programming philosophy, weakened the role of webform
the above are several feasible solutions discussed-both Asp.net webform and Asp.net MVC are feasible.

Another solution was found last night: Using ashx + jquery. ashx is a specialized file type used to process httphandler. It is used to process custom HTTP requests. config defines the HTTP request processing method for ashx when running.

<Add verb = "*" Path = "*. ashx "type =" system. web. UI. simplehandlerfactory "Validate =" false "/> so that we can use simplehandlerfactory to process the HTTP request of ashx. Implement the irequiressessionstate interface in the ashx class. You can use the session in system. Web. sessionstate under using, which is very convenient.

Using system. Web. sessionstate;
Public class checkcookie: ihttphandler, irequiressessionstate
{
... // Todo somthing
} Instance: Use ashx + jquery to verify the existence of email

. Ashx File

<% @ Webhandler Language = "C #" class = "checkuser" %>
 
Using system;
Using system. Web;
 
 
Public class checkuser: ihttphandler {
 
Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. Write (userrule. getinstance (). isuserexist (context. request ["email"]);
}
 
Public bool isreusable {
Get {
Return false;
}
}
} HTML:

<Input type = "text" id = "email"/>
<Input type = "button" value = "test" onclick = "check_email ()"/> JS:

function check_email () {
var email = $ ("# email "). ATTR ("value");
$. get (".. /ajax/checkuser. ashx ",
{Email: email},
function (data) {
window. alert (data);
});
}simple, obviously high efficiency. However, simple can only do something simple. It is not convenient to output HTML. If HTML is to be output, I prefer to use ascx for content processing. Therefore, ashx + jquery is a lightweight solution in Asp.net.

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.