Search code examples
c#asp-classiciis-7.5

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


For the past 2 days I'm trying to get my custom HTTP handler to work, but no result. I get the following error:

Could not load type 'AlarmHandler'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Could not load type 'AlarmHandler'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I followed several tutorials, but i guess I'm missing something small. I'm using the following configuration:

  • IIS 7.5
  • DefaultPool is set to Integrated mode
  • All files are in the root directory (C:\inetpub\wwwroot)
  • NO Handler mapping defined in IIS7.5
  • Web Project

AlarmHandler.ashx.cs:

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; }
    }
}

alarms.ashx:

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

web.config:

<configuration>
    <system.webServer>
        <handlers>
            <add name="AlarmHandler" path="*.ashx" verb="*" type="IHttpHandler" />
        </handlers>
    </system.webServer>
</configuration>

Solution

  • 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.