IIS 7.5 Unable to load custom HTTP handler with codebehind file

For the past 2 days, I have been trying to get my HTTP handler to work, but there is no result. I get the following error:

Failed to load type "AlarmHandler". Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and where it originated from the code.

Exception Details: System.Web.HttpException: Failed to load type 'AlarmHandler'.

Source Error:

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

I followed several textbooks, but I think I have something small. I am 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> 
+4
source share
2 answers

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 the AlarmHandler declared.

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

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

As an aside, I often used HTTP handlers and never bothered to register them (in my case, I usually call them explicitly through Ajax), so this line may not even be necessary.

Edit:

In this case, you are not using Visual Studio, which is slightly different from the fact that you will not have a bin directory, so we will have to do something different with the handler.

Your handler is currently split into an ASHX and CS file. This is usually normal, but in your case we will need to combine them.

This should be the contents of your Alarms.ashx file (you no longer need the AlarmHandler.ashx.cs file):

 <% @ 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 a third-party guide that you followed, it was almost certainly assumed that you were using Visual Studio, which might explain some of the difficulties you encountered.

+3
source

Just because this happens on a Google search for httphandlers and codebehind files: all you have to do is delete the .cs file in the App_Code folder and then reference the class from the .ashx file.

+2
source

Source: https://habr.com/ru/post/1445462/


All Articles