Initial ASP. NET --- general processing program, initial asp.net --- Processing

Source: Internet
Author: User

Initial ASP. NET --- general processing program, initial asp.net --- Processing
Source:

Today, I am typing a small demo and using Jquery to implement cascading drop-down boxes. During the knocking process, I find that no matter how it compares with the source code, I cannot display the desired functions. I thought that I didn't write background code. When I asked Yi Qing, he told me that I could use the general ASP. NET processing program.

I am a beginner in cainiao, because I have not touched the general processing program!

What is a general handler:

General handler: a special class that implements the System. Web. IHttpHandler interface. Any class that implements the IHttpHandler interface is a prerequisite for the target program of an external request. (Any class that does not implement this interface cannot be requested by the browser .) It is called and started to run by servers that support ASP. NET. An HttpHandler program is responsible for processing the access requests of one or more of its corresponding URLs, receiving the access request information (Request Message) sent by the client and generating response content (Response Message ).

Advantages: lightweight and efficient

Creating a general handler will generate two suffixed files. ashx and. ashx. cs. There is only one instruction set in ashx and no other code. ashx. cs is the page processing code. If there is a large amount of html, you need to use ashx to concatenate strings or read templates. The content of. ashx. cs is as follows:

Using System; using System. collections. generic; using System. linq; using System. web; namespace web {// <summary> /// Handler abstract description /// </summary> public class Handler: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; // indicates the output result type context. response. write ("Hello World"); // output result} public bool IsReusable {get {return false ;}}}}

(1) HttpContext: Request context object, including: HttpRequest, HttpResponse, Server, and Session.

(2) After the FrameWork receives a request, it creates an object of the class corresponding to the Request Path through reflection technology and forcibly converts it to an IHttpHandler interface object (HttpHandler implements IHttpHandler, the subsequent conversion is equivalent to converting the subclass into the parent class) and calling the ProcessRequest method for processing.
(3) The IsReusable attribute indicates whether the Handler can be stored in the pool for reuse when the IHttpHandlerFactory object is created.

(4) The ContentTyp attribute is the response content output by a general handler. The output content may be images, html, and other pages. The browser does not know what it receives based on the response content, you need to determine the ContentType in the response message header to determine the format of the report content. (Image/jpeg ---- indicates the image is output, and text/plain -------- indicates the output text)

 

From the code above, the general handler is actually a handler class that implements the IHttpHandler interface, which is directly mapped to the request with the ashx extension by ASP. NET in the system configuration file. In this way, we do not need to configure in the configuration file.

Specific purpose:

The HttpHandler program can complete most of the tasks that can be completed by common class programs:

   1. Obtain the data and URL parameters submitted by the client through the HTML Form

   2. Create Response Message content for the client

   3. access the file system on the server

   4. Connect to the database and develop database-based applications

   5. Call other classes

 

Using System; using System. collections. generic; using System. linq; using System. web; namespace web {// <summary> /// Handler abstract description /// </summary> public class Handler: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; // indicates the output result type context. response. write ("Hello World"); // output result} public bool IsReusable {get {return false ;}}}}

Specific application:

The following describes how to use Jquery to implement the cascade drop-down box. You need to use a general handler to obtain the code of a general handler of the automobile type.

Public class Handler1: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string strCarName = context. request ["carnameValue"]. toString (); SqlConnection conn = new SqlConnection ("server = .; database = car; uid = sa; pwd = 123456; "); // define the connection string conn. open (); // Open the connection SqlDataAdapter sdr = new SqlDataAdapter (); sdr. selectCommand = new SqlCommand ("selectid, cartype from t_cartype where carnameid = '" + strCarName + "'", conn); DataTable dt = new DataTable (); sdr. fill (dt); string str = DataTableToJson ("1", dt); context. response. write (str);} public bool IsReusable {get {return false ;}}}

Summary:

In fact, theoretical knowledge will remain at the theoretical level even if it is used. Programming is such a strange thing, no matter how detailed someone else talks, it is not as good as a practice. The demo Source Code mentioned in this article will end with a download link.

 

Demo source code download: http://download.csdn.net/detail/senior_lee/7697243


What is the use of aspnet in general processing programs?

It is mainly used to generate dynamic images and dynamic text, which is more efficient than traditional aspx files because it does not include the process of control parsing and page processing .. it is used for ajax to generate dynamic text. You can also use it for verification code images .. but its role should be more than that. I have read the following documents and said that it can completely customize Http requests. These are of course relatively advanced content.

Aspnet for general program Problems

Yes
Context. Request. QueryString ["id"]


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.