Lightweight asp. net ajax solution details

  • 2021-07-13 05:04:28
  • OfStack

This article illustrates a lightweight asp. net ajax solution. Share it for your reference, as follows:

I studied and discussed the ajax solution in asp. net except the official huge asp. net ajax with shotdog teacher. Our idea is to output in different server-side ways, and then call several server-side solutions on the page using ajax implementation of jQuery:

1. Use 1-like webform, call with jQuery ajax on the page, and then get it from the obtained html data < body > Write the contents in DOM

Advantages: No need to change the existing asp. net development mode, you can use ready-made pages; The content obtained by ajax is html text, which can be directly written into DOM

Disadvantages: Waste of content, < body > Anything other than MasterPage is not necessary, and if MasterPage is used. . .

2. Use webform like 1, but use Response. Write () to control the output html, call jQuery ajax on the page, and write the obtained content into DOM

Advantages: The content is clean and not wasted; The content obtained by ajax is html text, which can be directly written into DOM

Disadvantages: html text needs to be constructed in the form of string on the server side, which is inconvenient to program and difficult to debug and maintain

3. webform is used, Response. Write () is used to control the output of json data, jQuery ajax is used on the page, json data is processed into html at the client and written into DOM

Advantages: Only json data is exchanged, which is extremely clean and conforms to the efficient web design concept

Disadvantages: Need to process json data on the client side and cause intrusion into DOM

4. Use asmx, encapsulate web service, use jQuery ajax to call the content of asmx, and write DOM after json or xml data are processed into html by client

Advantages: Only exchange json or/xml data, very clean; web service is easy to cross platforms

Disadvantages: json data needs to be processed on the client side, and DOM is invaded

5. Use custom control ascx, then use special webform page to make wrapper (wrapper). Call wrapper webform with jQuery ajax on the page, and write html data into DOM

Advantages: webform is only used as wrapper, and custom controls can be dynamically used in wrapper according to different request parameters; The user-defined control outputs html text, which can be directly written into DOM; Easy programming, VS2008 code-aware support, easy debugging and maintenance

Disadvantages: Different from the traditional webform programming concept, it weakens the role of webform

These are the possible solutions discussed-whether asp. net webform or asp. net MVC.

Last night, another scheme was discovered: using ashx + jQuery. ashx is a file type specially used to handle HttpHandler, which is used to handle custom Http requests. web. config can define the processing mode of Http requests for ashx at runtime.

<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory"validate="false" />

In this way, we can use SimpleHandlerFactory to process http requests from ashx. Realize IRequiresSessionState interface in ashx class, System. Web. SessionState under using can use Session, very convenient


using System.Web.SessionState;
public class checkCookie : IHttpHandler ,IRequiresSessionState
{
  ... // todo somthing
}

Example: Using 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 the efficiency will be higher. However, simple can only do something like simple. If you want to output html, it is still inconvenient. If you want to output html, I prefer to use ascx for content processing and webform for packaging, so ashx+jQuery should be regarded as a lightweight solution in asp. net

I hope this article is helpful to everyone asp. net programming.


Related articles: