IIS 7.5 Can't load custom HTTP Handler with codebehind file

馋奶兔 提交于 2019-12-01 18:52:37

What worked for me was changing:

<% @ WebHandler language="C#" class="AlarmHandler" codebehind="AlarmHandler.ashx.cs" %>

To:

<% @ WebHandler language="C#" class="Namespace.AlarmHandler" codebehind="AlarmHandler.ashx.cs" %>

Where Namespace is the namespace in which AlarmHandler is declared.

With this in mind, I would think that changing the handler registration to this might be a good idea:

<add name="AlarmHandler" path="*.ashx" verb="*" type="Namespace.AlarmHandler" />

As an aside, I have used HTTP handlers on many occasions and have never bothered to register them (in my case I tend to explicitly invoke them via Ajax), so this line may not even be neccessary.

Edit:

In this case you are not using Visual Studio, which makes things a little different in that you won't have a bin directory, so we will have to do things a bit differently with the handler.

At the moment your handler is split across an ASHX and a CS file. This would normally be fine, but in your case we will need to combine them.

This should be the contents of your Alarms.ashx file (you won't need the AlarmHandler.ashx.cs file anymore):

<% @ WebHandler language="C#" class="AlarmHandler" %>

using System.Web;

public class AlarmHandler : IHttpHandler
{
    // Constructor.
    public AlarmHandler() { }

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest Request = context.Request;
        HttpResponse Response = context.Response;

        // Test code.
        Response.Write("<html>");
        Response.Write("<body>");
        Response.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");
        Response.Write("</body>");
        Response.Write("</html>");
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

As an aside, the tutorials you have been following would almost certainly have assumed that you were using Visual Studio, which might explain some of the difficulty you encountered.

Just because this comes up in a Google search about httphandlers and codebehind files: all you need to do is drop the .cs file in an App_Code folder and then reference the class from the .ashx file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!