简体   繁体   English

IIS 7.5无法使用代码隐藏文件加载自定义HTTP处理程序

[英]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. 在过去的两天里,我试图让我的自定义HTTP处理程序工作,但没有结果。 I get the following error: 我收到以下错误:

Could not load type 'AlarmHandler'. 无法加载类型'AlarmHandler'。 Description: An unhandled exception occurred during the execution of the current web request. 描述:执行当前Web请求期间发生未处理的异常。 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'. 异常详细信息:System.Web.HttpException:无法加载类型'AlarmHandler'。

Source Error: 来源错误:

An unhandled exception was generated during the execution of the current web request. 在执行当前Web请求期间生成了未处理的异常。 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 IIS 7.5
  • DefaultPool is set to Integrated mode DefaultPool设置为集成模式
  • All files are in the root directory (C:\\inetpub\\wwwroot) 所有文件都在根目录(C:\\ inetpub \\ wwwroot)中
  • NO Handler mapping defined in IIS7.5 IIS7.5中定义的NO Handler映射
  • Web Project 网络项目

AlarmHandler.ashx.cs : 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 : alarms.ashx

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

web.config : web.config

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

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. 其中Namespace是声明AlarmHandler的名称空间。

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. 顺便说一句,我曾经多次使用过HTTP处理程序,并且从不打扰注册它们(在我的情况下,我倾向于通过Ajax显式地调用它们),所以这条线甚至可能不是必需的。

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. 在这种情况下,您不使用Visual Studio,因为您没有bin目录会使事情略有不同,因此我们将不得不使用处理程序做一些不同的事情。

At the moment your handler is split across an ASHX and a CS file. 目前,您的处理程序分为ASHX和CS文件。 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): 这应该是您的Alarms.ashx文件的内容(您将不再需要AlarmHandler.ashx.cs文件):

<% @ 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. 顺便说一句,您一直关注的教程几乎肯定会假设您使用的是Visual Studio,这可能会解释您遇到的一些困难。

只是因为这出现在关于httphandlers和代码隐藏文件的Google搜索中:您需要做的就是将.cs文件放在App_Code文件夹中,然后从.ashx文件中引用该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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