HTTP Handler (1)-create a simple HTTP Handler

Source: Internet
Author: User
What can I get?

After reading all the articles in the "HTTP Handler" series, you can
-Easily create your own custom HTTP handler
-Share the Session between your HTTP handler and the Web Handler
-Allows dynamically generated images to be directly displayed on the web page without the need for temporary file transfer.
-Send the dynamically generated PDF file directly to the Client without transferring temporary files.

What is an HTTP processing program?

You must have used RDLC reports? If you right-click a chart in the RDLC report and select "properties", you will find the chart in the RDLC report.
-A (dynamically generated) Image
-The image address is a bit strange. It is not "XX Folder \ XX image file", but similar to "Reserved. ReportViewerWebControl. axd? Mode = true & ReportID = e1eab20aaba... "is similar to a Request sent to a page. As shown in

However, there is no page named ReportViewerWebControl. axd on our website. That's right, ReportViewerWebControl. axd is not a page. It is an HTTP Handler that can accept access requests and respond to requests like a page, but is more "bottom-layer" than a page. It means a more flexible HTTP processing program. If you want to learn more about HTTP Handler, you can refer to the Inside ASP. NET 2.0-instant compilation system of Huang zhongcheng. If you are as impatient as the author and do not need to solve the problem, you can directly Follow me and create a custom HTTP Handler in 10 minutes.

Create a simple HTTP Handler

Step 1: create a Web application in VS2005.

Step 2: Create a class library and set the namespace to "mylib. system. web". This namespace can be set to another value as needed.
Open Solution Explorer, right-click solution HTTPHandlerDemo, select Add> Create project..., and select class library ".
Note: If you create a Web program of VB in step 1, "solution Resource Manager" may not display "solution HTTPHandlerDemo ", in this case, you can choose File> Add... -> Create a project... -> class library "to create a class library.

Step 3: Add a reference to System. Web for the newly created class library.

Step 4: Create a class named "MyHandler. Similarly, this class name is random.

Step 5: Modify the MyHandle class so that it can output some information to the client. MyHandler. cs
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace mylib. system. web
{
Public class MyHandler: System. Web. IHttpHandler
{
# Region IHttpHandler Member

Public bool IsReusable
{
Get {return false ;}
}

Public void ProcessRequest (System. Web. HttpContext context)
{
Context. Response. ContentType = "text/html ";
Context. Response. Write ("}

# Endregion
}
}

Step 6: Add a reference to the class library "mylib. system. Web" for the web application "HTTPHandlerDemo" created in step 1.

Step 7: Modify the Web of the Web application created in step 1. in the "system. add a line of "httpHandlers" in the web section, as shown in the following code (painted with a green background ). Note that I wrote "path =" MyHandler. jxd "". Why is the extension "jxd? My name is Jing, so it's "jxd". If your name is Wang, you can call it "MyHandler. wxd ".

"Web. config" is not displayed in "solution manager? The simplest way is to press F5 to run the program, and then click OK in the pop-up dialog box "modify Web. config. Web. config
<? Xml version = "1.0"?>
<! --
Note: In addition to manually editing this file, you can also use
Web management tools to configure application settings. You can use
"Website"-> "Asp. Net configuration" option.
The complete list of settings and comments is displayed in
In machine. config. comments, this file is usually located in
\ Windows \ Microsoft. Net \ Framework \ v2.x \ Config
-->
<Configuration>
<AppSettings/>
<ConnectionStrings/>
<System. web>
<HttpHandlers>
<Add path = "MyHandler. jxd" verb = "*" type = "mylib. system. web. MyHandler, mylib. system. web"
Validate = "false"/>
</HttpHandlers>
<! --
Set compilation debug = "true" to insert the debugging symbol
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation debug = "true"/>
<! --
In the <authentication> section, you can configure
Security Authentication mode,
To identify the user.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
Html error page to be displayed
To replace the error stack trace.

<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>
-->
</System. web>
</Configuration>

Step 8: test. Press F5 to run the program. In the IE Address Bar, manually change the address to "/HttpHandlerDemo/MyHandler. jxd" and press enter to view "Hello!" displayed on the page !" .


Step 9: Modify the "Default. aspx" page of the Web application and add an IFrame tag to display the content in MyHandler. jxd. Default. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Iframe src = '~ /MyHandler. jxd '> </iframe>
</Div>
</Form>
</Body>
</Html>

Set Default. aspx as the start page and press F5 to run the program. You can see "Hello!" in IFrame !" .

Step 10: publish a program and configure IIS. Publish the Web application and create a virtual directory in IIS. When you browse Default. aspx, you will find that the error message "webpage not found" is displayed in IFrame. This is because IIS does not know which program to use to process access requests with the extension "jxd. Follow these steps to add the application ing.
First open IIS, right-click on the virtual directory, and select "properties ".

Click "Configure. Add a ". jxd" ing to the executable file on the "Application configuration" page. The "executable files" column can be entered in the "executable files" column of ". axd.

Download all source code in this article

This article ends here. The next article describes how to pass parameters to MyHandler. jxd.

 

6 articles in this series
Practical HTTP Handler (HTTP Handler) (6) -- random bar code
Actual HTTP Handler (HTTP Handler) (5) -- directly open the dynamically generated file without using temporary files
Practice HTTP Handler (4) -- share Session with Web programs
HTTP Handler (3) -- dynamically generate images
HTTP Handler (2) -- passing parameters to the HTTP Handler
HTTP Handler (1) -- create the simplest HTTP Handler<-You are here.

 

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.