In the previous blog, it was really unusual to mention general handlers. This blog is mainly for everyone to dispel doubts. Because recently in the process of doing projects, the use of a lot of general procedures, I believe you must have used, but you really know it?
The general handler is generally relative to the ASPX page, which is between the request and the response handler. To be blunt is to remove the foreground appx HTML page CS Part, what request can handle, realize IHttpHandler interface.
Creating a generic handler through VS2012 will generate a file of two suffix names. ashx and. Ashx.cs. Where. Ashx.cs corresponds to the content, such as the following code:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Collections.Generic; using Syste M.linq; Using System.Web; Namespace Web { ///<summary> ///Handler Summary description ///</summary> public class Handler:ihttphandler { public void ProcessRequest (HttpContext context) { context. Response.ContentType = "Text/plain"; context. Response.Write ("Hello World"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC bool isreusable { get { return false; } } }} |
As you can see from the above code, the generic handler is actually a handler class that implements the IHttpHandler interface, which is asp.net directly on the system configuration file to the ASHX extension request. In this way, we do not need to configure in the configuration file.
Because you do not have to inherit the page class, there are not so many events to process, less consuming resources, and higher performance than ASPX.
General handlers can do a lot of functions, output HTML, the effect is the same as Aspx+cs, can also be used to send a non-HTML class or the need for HTML code data, such as text, XML, pictures, even binary file format, as well as the most commonly used JSON data format.
Simply give the three most common examples:
1, Picture filter (anti-theft chain)
2. Picture Verification Code
3, processing the custom suffix name request
This three example code, I through the reprint of the blog, for you to do a detailed introduction.
In the past we are from a functional perspective to learn and think about how a page to use, with continuous learning, I think more should be from the principle and the essence of understanding and learning, from the above reproduced blog for everyone to solve this problem.