Asp. Net HttpHandler

Source: Internet
Author: User

For example: Output xmlDom, logout and jump, there is no html output, it is very troublesome, you need to create a page, delete unnecessary html, and write processing code in page_load. The use of HttpHandler does not need to be so troublesome.
You can use any language that complies with common language standards (CLS) to write custom HTTP handlers to process specific and predefined types of HTTP requests. The executable code defined in the HttpHandler class is used to respond to these specific requests, rather than the conventional ASP or ASP. NET Web pages. The HTTP processing program provides you with a way to interact with low-level requests and Response Services of the IIS Web server. It also provides functions that are very similar to ISAPI extensions but simple programming models.
For example, I need to implement a Logout. aspx page to log out and jump to. The following example mainly responds to the client's request for a page named logout. aspx to log out and jump. All requests to logout. aspx are provided by LogoutHttpHandler in WebUC. HttpHandler, The namespace in WebUC. dll of the Assembly.
Modify web. config and add the following script to <system. web> </system. web>:
<HttpHandlers>
<Add verb = "GET" path = "Logout. aspx" type = "WebUC. HttpHandler. LogoutHttpHandler, WebUC"/>
</HttpHandlers>
Among them, WebUC. HttpHandler. LogoutHttpHandler is the class for implementing the Logout. aspx function. WebUC is the dll of my web project. (For details, refer to msdn)
The following is the code of LogoutHttpHandler. It inherits the excuse to rewrite methods and attributes.
Copy codeThe Code is as follows:
Using System;
Using System. Web;
Using System. Web. Caching;
Using System. Web. Security;
Namespace WebUC. HttpHandler
{
Public class LogoutHttpHandler: IHttpHandler
{
/// <Summary>
/// Enable HTTP Web request processing by implementing the custom HttpHandler interface of the IHttpHandler interface.
/// </Summary>
/// <Param name = "context"> HttpContext object, which provides reference for internal Server objects (such as Request, Response, Session, and Server) used to provide services for HTTP requests. </Param>
Public void ProcessRequest (HttpContext context)
{
FormsAuthentication. SignOut ();
Context. Response. Redirect ("Login. aspx", true );
}
/// <Summary>
/// Obtain a value indicating whether other requests can use the IHttpHandler instance.
/// </Summary>
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}
}

After compilation, I can directly use http: // ***/logout. aspx is used to log out. In fact, there is no logout in my web directory. the aspx file can also be used in many aspects, such as preventing leeching and downloading statistics.

Related Article

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.