gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:07:10 29 4
gpt4 key购买 nike

在过去的 2 天里,我试图让我的自定义 HTTP 处理程序正常工作,但没有结果。我收到以下错误:

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.

我学习了几个教程,但我想我遗漏了一些小东西。我正在使用以下配置:

  • IIS 7.5
  • DefaultPool 设置为集成模式
  • 所有文件都在根目录(C:\inetpub\wwwroot)
  • IIS7.5 中没有定义处理程序映射
  • 网络项目

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>

最佳答案

对我有用的正在改变:

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

收件人:

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

其中 Namespace 是声明 AlarmHandler 的命名空间。

考虑到这一点,我认为将处理程序注册更改为此可能是个好主意:

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

顺便说一句,我曾多次使用 HTTP 处理程序,但从来没有费心去注册它们(在我的例子中,我倾向于通过 Ajax 显式调用它们),所以这一行甚至可能不是必需的。

编辑:

在这种情况下,您没有使用 Visual Studio,这使得事情有点不同,因为您没有 bin 目录,所以我们必须对处理程序做一些不同的事情。

目前您的处理程序分为 ASHX 和 CS 文件。这通常没问题,但在您的情况下,我们需要将它们结合起来。

这应该是您的 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; }
}
}

顺便说一句,您一直在学习的教程几乎肯定会假定您使用的是 Visual Studio,这可能会解释您遇到的一些困难。

关于c# - IIS 7.5 无法使用代码隐藏文件加载自定义 HTTP 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340635/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com