Initial ASP---Generic handler

Source: Internet
Author: User

Source of the problem:

Today in a small demo, using jquery to achieve cascading drop-down box, the process of knocking found no matter how and source control can not display the desired function. This is to think of the original is not written backstage code, asked a clear classmate, he told me can use the general process of ASP.

I am really a novice beginner, because I have not been exposed to the general processing procedures!

what is a generic handler:

Generic handler: is a special class that implements the System.Web.IHttpHandler interface. Any class that implements the IHttpHandler interface is a precondition for the target program as an external request. (Any class that does not implement this interface cannot be requested by the browser.) It is called and started by a server that supports ASP. A HttpHandler program is responsible for processing access requests for one or a set of URL addresses that it corresponds to, and receiving access request information (Request messages) from the client and generating response content (response messages).

Advantages: Light weight, high efficiency

Creating a generic handler will generate a file with two suffix names. ashx and. Ashx.cs. There is only one instruction set in ashx, no other code; Ashx.cs is the page processing code. If you have a lot of HTML, you need to ashx the string or read the template. Where. Ashx.cs corresponds to the content, such as the following code:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Web;    Namespace Web  {      //<summary>///Handler//</summary> public class Handler: IHttpHandler      {public          void ProcessRequest (HttpContext context)          {              context. Response.ContentType = "Text/plain";  Represents the output result type            context. Response.Write ("Hello World");  Output        } public            bool isreusable          {              get              {                  return false;  }}}  

(1) HttpContext: Request Context object, contains: Request Message Object (HttpRequest), Response message object (HttpResponse), Server helper Class (server), session, etc.

(2) After receiving a request, the framework creates an object of the class that corresponds to the request path through the reflection technology and casts it to the IHttpHandler interface object (HttpHandler implements the IHttpHandler, after which the conversion is the equivalent of strongly converting the subclass to the parent class). Unified call ProcessRequest method processing.
(3) The IsReusable property is used to indicate whether this handler can be pooled for reuse when the IHttpHandlerFactory object is created IHttpHandler.

(4) The Contenttyp property is the general handler output response content, the content of the output may be pictures, HTML and other pages, the browser according to the response to the style content does not know what is received, It is necessary to judge the contenttype of the response message header to decide what format data to display as the style content of the newspaper. (image/jpeg----Indicates that the output is a picture, Text/plain--------represents the output text)

As you can see from the code above, the generic handler is actually a handler class that implements the IHttpHandler interface, which is directly mapped to the system configuration file by ASP. NET on the request to the ashx extension. This way, we don't need to configure it in the configuration file.

specific uses:

The HttpHandler program can accomplish most of the tasks that ordinary class programs can accomplish:

1 . get the data and URL parameters submitted by the client via HTML form form

2. Create a response message content to the client

3. Accessing the server-side File system

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

5. Calling other classes

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Web;    Namespace Web  {      //<summary>///Handler//</summary> public class Handler: IHttpHandler      {public          void ProcessRequest (HttpContext context)          {              context. Response.ContentType = "Text/plain";  Represents the output result type            context. Response.Write ("Hello World");  Output        } public            bool isreusable          {              get              {                  return false;  }}}  

Specific application:

The following is the code that uses jquery to achieve a generic handler for the type of car that is used in a cascading drop-down box effect.

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; ");   Defines the connection string            Conn. Open ();     Open 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, the theoretical knowledge of the good will not be used will only stay on the theoretical level. And programming is such a strange thing. No matter how detailed the others speak, they are inferior to one's own practice. The text mentioned in the demo source, the end of the article will be accompanied by a download link.

Demo Source Download: http://download.csdn.net/detail/senior_lee/7697243

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.