Ashx + jquery, a lightly Ajax solution of Asp.net

Source: Internet
Author: User
We 1) output data in different server methods, and then use jquery Ajax implementation on the page to call the server. In this way, there are several Ajax solutions different from the official huge Asp.net Ajax:
  1. Use a common webform and call it with jquery Ajax on the page. Then, obtain the content in <body> from the obtained HTML data and write it to the Dom.
    • Advantage: 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 to the Dom.
    • Disadvantage: A waste of content. content other than <body> is not necessary, and if masterpage is used, then...
  2. Use a common webform, but use response. Write () to control the output html. Use jquery ajax to call the webpage and write the obtained content to the Dom.
    • Advantage: The content is clean and not wasted. The content obtained by Ajax is HTML text, which can be directly written to Dom.
    • Disadvantage: HTML text needs to be constructed in string form on the server side, which is inconvenient for programming and is not easy to debug and maintain.
  3. Use a common webform and use response. Write () to control the output of JSON data. Call jquery Ajax on the page to process JSON data on the client and write it to the Dom.
    • Advantage: Only JSON data is exchanged, which is extremely clean and conforms to the efficient web design concept.
    • Disadvantage: JSON data needs to be processed on the client and Dom intrusion is caused.
  4. Use asmx to encapsulate the data into a web service. Use jquery ajax to call the content of asmx, process JSON or XML data on the client as HTML, and write it to the Dom.
    • Advantages: Only JSON or XML data is exchanged, which is very clean; web service is easy to cross-platform
    • Disadvantage: JSON data needs to be processed on the client and Dom intrusion is caused.
  5. Use the custom control ascx, and then use a dedicated webform page for wrapper (packaging). On the page, 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 into Dom. It is easy to program, with vs2008CodePerception support, easy debugging and Maintenance
    • Disadvantage: different from the traditional webform programming philosophy, it weakens the role of webform.

The above are several feasible solutions discussed-either Asp.net webform or Asp.net MVC.

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. You can define the HTTP request processing method for ashx when running in Web. config.

<Add Verb="*" Path="*. Ashx" Type="System. Web. UI. simplehandlerfactory" Validate="False" />

In this way, we can use simplehandlerfactory to process the HTTP request of ashx.
Implement the irequiressessionstate interface in the ashx class. You can use the session by using the using system. Web. sessionstate, which is very convenient.

 
Using System. Web. sessionstate;
Public ClassCheckcookie:Ihttphandler, irequiressessionstate
{
// Todo somthing
}

Example: 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 ) ;
} ) ;
}

Obviously, the efficiency is relatively high. However, simple can only do something simple. It is not convenient to output HTML. If HTML is to be output, I still prefer to use ascx for content processing, and webform for packaging. So 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.