HttpHandler in the magical asp.net

Source: Internet
Author: User
Tags http request httpcontext implement
asp.net

Many times, we create a new xxx.aspx page and Xxx.aspx.cs file, but in order to achieve a very simple function, such as: Output xmldom, logout and jump, and no HTML output, very troublesome, need to create a new page, delete the extra HTML, and in page _load inside write processing code. And using HttpHandler doesn't have to be that much trouble.

You can write custom HTTP handlers in any language that conforms to the common Language Specification (CLS) to handle specific, predefined types of HTTP requests. In response to these specific requests, the executable code defined in the HttpHandler class is not a regular ASP or ASP.net Web page. The HTTP handler provides you with a way to interact with the low-level request and response services of the IIS Web server, while providing functionality that is extremely similar to the ISAPI extension but simpler to the programming model.

For example, I now need to implement a logout and Jump Logout.aspx page, the following example mainly implemented in response to the client to a page named logout.aspx, implementation logout and jump. All requests for logout.aspx are serviced by Logouthttphandler in the namespace Webuc.httphandler contained in the Assembly WebUC.dll.

Modify the web.config and add the following script in <system.web></system.web>:
<add verb= "Get" path= "logout.aspx" type= "WebUC.HttpHandler.LogoutHttpHandler, Webuc"/>
Where WebUC.HttpHandler.LogoutHttpHandler is the class that I want to implement logout.aspx functionality, Webuc is the DLL for my Web project. (Refer to MSDN for a specific introduction)

Here's the code for Logouthttphandler, an inheritance excuse, overriding methods and attributes.

using System;
using system.web;
using System.Web.Caching;
using System.Web.Security;

namespace Webuc.httphandler
{

Public class Logouthttphandler:ihttphandler
{
<summary>
Enable processing of HTTP WEB requests by implementing a custom HttpHandler of the IHttpHandler interface.
</summary>
<param name= The "context" >httpcontext object, which provides references to internal server objects (such as requests, Response, sessions, and servers) that are used to provide services for the HTTP request. </param>
Public void ProcessRequest (HttpContext context)
{
FormsAuthentication.SignOut ();
Context. Response.Redirect ("Login.aspx",true);
}

<summary>
Gets a value that indicates whether the IHttpHandler instance can be used by other requests.
</summary>
Public BOOL IsReusable
{
Get
{
return false;
}
}
}

}
}

After compiling, I can use http://***/logout.aspx directly to implement logoff,
In fact, my web directory does not logout.aspx this file, again,
This technique can be used in a number of ways, such as preventing hotlinking, downloading statistics, and so on.



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.