Use HttpHandler to exit without pages and prevent leeching

Source: Internet
Author: User
Many times, we create a new xxx. aspx page and xxx. aspx. the cs file is used to implement a very simple function, such as outputting xmlDom, logging out and redirecting, and having no html output. It is very troublesome to create a new page and delete unnecessary html, write the 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.

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.

Note:

What does httphandler do? I won't say much about finding a lot of materials using any search engine. I will write some of my current learning experiences here, hoping to get to know httphandler from the other side.
The so-called httphandler is actually used by. net to process page requests. You can think about it. Only files with the. aspx suffix can be protected by forms authentication, while files with the. config suffix are not accessible. All of these are handled by some httphandler built in asp.net. The reason why we need to learn to use httphandler is mainly because we want to handle some http requests on our own.
Search for "httphandler. net" from google. The following are the purposes of httphandler:
1. Prevent leeching. It mainly prevents image leeching.
2. Use url rewriting. I haven't looked at this carefully. The general meaning is that the Url can be switched to the correct address, for example, to shorten a long url so that a nonexistent url is redirected to an error or default address.
3. Perform short and convenient operations. For example, some articles say that httphandler is used to implement the logout function.
4. Wait.
As one of the core http processing capabilities of asp.net, httphandler has more functions than that. I recently want to learn httphandler mainly because of my job (I want to graduate, Hoho ...) you need to use this to process custom extension files, such as http: // localhost/hello. for a webpage such as notus, the suffix names of all pages on the site are used. notus, in order to use the custom page generation method (mainly when the page is generated using xml + xslt ).
I 've mentioned some of the functions of httphandler. Next I'll take a look at what I learned today. I dare say that there must be something you need, because it's something I 've learned for a long time (what, at that time, everyone knew it at first glance? Is it true that I am the dumbest ?)
Check. net sdk documentation, you will find that there are IHttpHandler and IHttpHandlerFactory, the first is the interface that needs to be implemented by the custom httphandler, followed by the interface that needs to be implemented by the custom factory that returns httphandler, these two items are on the web. the registration methods in config are the same. I am very familiar with the former. What is the factory behind?
The answer is to return an httphandler (......).
What does the returned httphandler do?
The answer is to process the current http request.
Since both processes the current http request, why not use IHttpHandler directly?
I don't know.
What is a factory?
Is it the factory model?
What is the factory model?
Appropriate products can be returned based on customer needs.
So what is the role of IHttpHandlerFactory?
Well, return the appropriate HttpHandler based on the current http request.
That's what it means. For example, http: // localhost/hello. the notus url is specially processed. when the request is sent to this Url, the factory returns the HttpHandler that processes hello separately, but other URLs, and returns a common HttpHandler.
The following is a simple code example:
System. Web. IHttpHandler GetHandler (System. Web. HttpContext context, string requestType, string url, string pathTranslated)
{
// Full name of the file
String fname = url. Substring (url. LastIndexOf ("/") + 1 );
// File name
String cname = fname. Substring (0, fname. IndexOf ("."));
// Return the appropriate HttpHandler as needed
If (cname = "hello ")
Return new HandlerHello ();
Else
Return new HandlerCommon ();
}

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.