简体   繁体   中英

HTTPHandler in asp.Net

I want to implement HTTPHandler in my asp.net project. I followed the links to do the same. I created a folder named App_code in my root. Them I wrote a class MyHTTPHandler. It has the Reusable property on, also I handles the Process

public class HelloWorldHandler : IHttpHandler
{
    public HelloWorldHandler()
    {
    }

    public void ProcessRequest(HttpContext context)
    {

        context.Response.ContentType = "text/plain";

        if (context.Request.RawUrl.Contains(".cspx"))
        {
            string newUrl = context.Request.RawUrl.Replace(".cspx", ".aspx");
            context.Server.Transfer(newUrl);
        }

    }

    public bool IsReusable
    {
        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}

The Handler is not getting fiered. As I am new to ASP.Net I m not able to figure out, what is going wrong. I also entered the part that is required in web.config. I went through many links, some says you need to copy the code in IIS. I am not able to understand it. Please advice

There is no need for any set up in IIS, unless you want to handle some path that is already regsistered. In general case all you need to do is to add the <httpHandlers> section to web.config:

<configuration>
  ...
  <system.web>
    ...
    <httpHandlers>
      <add verb="*"
           path="HelloWorldHandler.ashx" 
           type="NamespaceName.HelloWorldHandler, WebApplicationName" />
    </httpHandlers>
    ...
  </system.web>
  ...
</configuration>

Here HelloWorldHandler.ashx is a path that you need to use to fire the handler, NamespaceName.HelloWorldHandler is a full name of the handler class, including all namespaces, and WebApplicationName is the name of the assembly handler is implemented in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM