Extended HTTP Pipeline

Source: Internet
Author: User

The Application Event pipeline is not only limited to requests to. aspx Web forms, but also can be used to create your own handler to process custom file types.

Requests of all asp.net applications are processed by special components called HTTP handlers. The HTTP handler is the skeleton of the asp.net request processing framework.

Configuration in web. config

<HttpHandlers>
<Add verb = "*" path = "source. simple" type = "SourceHandler"/>
<Add verb = "*" path = "test. simple" type = "SimpleHandler"/>

....

 

If you want to work on lower layers outside of the web form model to support special format processing, you can implement your own HTTP processing program.

 

1. You can create a class to implement the IHttpHandler interface in the App_Code directory.

 

Code:

Using System;
Using System. Web;

 

Public class SimpleHandler: IHttpHandler
{

// ASP. NET will call this method to obtain the request
Public void ProcessRequest (System. Web. HttpContext context)
{
HttpResponse response = context. Response;
Response. Write ("Response. Write ("}

Public bool IsReusable
{
Get {return true ;}
}
}

If you create this extension in a class library project, you need to add a reference to the System. web. dll assembly, which contains a large number of ASP. NET classes.

 

2. configure a custom HTTP handler


Register your HTTP handler in the web. config file

<HttpHandlers>
<Add verb = "*" path = "source. simple" type = "SourceHandler"/>
<Add verb = "*" path = "test. simple" type = "SimpleHandler"/>

</HttpHandlers>

When registering an HTTP handler, you must specify three important links. Verb indicates whether the request is an http post or an http get request (using * to indicate all requests ).

The path feature indicates the extension of the file for which the HTTP handler will be called.

Finally, the type feature identifies the HTTP handler class.

 

This method has the disadvantages: when deploying an application to an iis web server, it is not convenient to use a custom HTTP handler.

Because IIS5 or IIS6 cannot recognize. simple extensions or are not aware of their association with ASP. NET. On the contrary, IIS only checks the file named test. simple.

To change this behavior, you need to manually add an IIS ing.

 

3. register an HTTP handler without configuring IIS


ASP. NET also provides another method for registering an HTTP handler-using a recognizable extension. ashx. All requests ending with ashx are automatically recognized as requests for custom HTTP handlers.

The. ashx file starts with the webHandle command. The following example:

<% @ WebHandler Language = "C #" class = "SourceHandler" %>

Most importantly, the. ashx file type has been registered in IIS, so you do not have to execute any IIS configuration when publishing an application.

Whether to use the configuration file or the. ashx file is just a matter of personal preference. However, the. ashx file is usually used for simple extensions designed for a single Web application.

Configuration files give you more flexibility

The SourceHandler class is located in the App_Code directory.

Example of this class:

Public class SourceHandler: IHttpHandler
{
Public void ProcessRequest (System. Web. HttpContext context)
{
// Make the HTTP context objects easily available.
HttpResponse response = context. Response;
HttpRequest request = context. Request;
HttpServerUtility server = context. Server;

 

Response. Write ("

// Get the name of the requested file.
String file = request. QueryString ["file"];
Try
{
// Open the file and display its contents, one line at a time.
Response. Write ("<B> Listing" + file + "</B> <br> ");
StreamReader r = File. OpenText (server. MapPath (Path. Combine ("./", file )));
String line = "";
While (line! = Null)
{
Line = r. ReadLine ();

If (line! = Null)
{
// Make sure tags and other special characters are
// Replaced by their corresponding HTML entities, so they
// Can be displayed appropriately.
Line = server. HtmlEncode (line );

// Replace spaces and tabs with non-breaking spaces
// To preserve whitespace.
Line = line. Replace ("", "& nbsp ;");
Line = line. Replace ("\ t", "& nbsp ;");

// A more sophisticated source viewer might apply color-coding.
Response. Write (line + "<br> ");
}
}
R. Close ();
}
Catch (ApplicationException err)
{
Response. Write (err. Message );
}
Response. Write ("}

Public bool IsReusable
{
Get {return true ;}
}
}

This code finds the requested file, reads its content, and uses a string replacement and HTML encoding to create a content that can be safely displayed by the browser.

Then, you also need to map the handler to the file extension,

<HttpHandlers>
<Add verb = "*" path = "source. simple" type = "SourceHandler"/>

<HttpHandlers>

Http: // localhost: 1390/Website/source. simple? File = HolmesQuote. aspx. cs

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.