Building an HTTP handler in a class library is simple, adding an ordinary class, and then pasting the code in the previous ashx almost exactly as it is in this class. But pay attention to namespaces and class names, because we'll use them later.
Sample handler:
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 to implement: IsReusable and ProcessRequest the members defined in both interfaces.
Below we will go to the site's web. config to register this handler. If you are using IIS7 and above, and use the integrated mode, it is necessary to configure:
Under the System.webserver\handlers node, add:
<add name= "Examplehandler" verb= "*" path= "Ex.axd" type= "EdiBlog.Core.Web.HttpHandlers.ExampleHandler, Ediblog.core "/>
Where path is the path to access handler, and the extension needs to be registered in IIS, if you are using a virtual host like me, and you cannot manage IIS yourself, do not use an extension name that is not supported by IIS, such as ABC.
The 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 access the handler on the webpage!
asp: A concrete way to write ashx into a class library and invoke it on a page