ASP. NET: The specific method that writes ashx to the class library and calls it on the page

  • 2020-06-15 08:01:10
  • OfStack

Building Http Handler into the class library is as simple as adding a generic class and then attaching almost a copy of the previous ashx code to the class. But pay attention to the namespace and class names, because we'll use them later.
The 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 ...
        }
    }
}

This handler logic doesn't matter, you can define it yourself. The key is the implementation: the members defined in the IsReusable and ProcessRequest interfaces.
Let's go to web. config to register this handler. If you are using IIS7 or above and use integration mode, configure it this way:
Add under the system. webServer\handlers node:
< add name="ExampleHandler" verb="*" path="ex.axd" type="EdiBlog.Core.Web.HttpHandlers.ExampleHandler, EdiBlog.Core" / >

path is the path to access handler, and the extension needs to be registered in iis. If you and I are using a virtual host and cannot manage IIS by ourselves, please do not use an extension that is not supported by abc by default.
There are two arguments in type, the first for the full name of the handler class and the second for the assembly name.
Now we can access handler on the web using ex.axd!


Related articles: