The simple way to build an HTTP handler in a class library is to add an ordinary class, and then paste the code from the previous ashx almost exactly the same into the class. But be aware of namespaces and class names, because we'll use them later.
Sample handler:
Copy Code code as follows:
Namespace EdiBlog.Core.Web.HttpHandlers
{
Using System;
Using System.Web;
public class Examplehandler:ihttphandler
{
public bool IsReusable
{
get {return false;}
}
public void ProcessRequest (HttpContext context)
{
Your own logic ...
}
}
}
The logic of this handler is not important, you can define it yourself. The key point is implementation: IsReusable and ProcessRequest the members defined in these two interfaces.
Below we will go to the site's Web.config to register this handler. If you are using IIS7 and the above version, and the integration mode, you need to configure this:
Add under the System.webserver\handlers node:
<add name= "Examplehandler" verb= "path=" Ex.axd "type=" EdiBlog.Core.Web.HttpHandlers.ExampleHandler, Ediblog.core "/>
Where path is access to handler, and the extension needs to be registered in IIS, and if you are using a virtual host like me, and you cannot manage IIS yourself, do not use the extension name that IIS does not support by default, such as ABC.
Type has two parameters, the first is the full name of the handler class, and the second is the name of the assembly.
Now we can use Ex.axd to visit handler on the website!