我遇到了构建的自定义错误处理程序的问题。它应该是HttpModule
,但是当我将它添加到我的web.config
的system.webServer/modules
标记中时,它不会启动。用于集成的IIS 7的自定义HttpModule
这是我web.config
部分:
<system.webServer>
<modules>
<add name="AspExceptionHandler"
type="Company.Exceptions.AspExceptionHandler, Company.Exceptions"
preCondition="managedHandler" />
</modules>
</system.webServer>
这是我HttpModule
代码:
using System;
using System.Web;
using Company.Settings;
using System.Configuration;
namespace Company.Exceptions
{
public class AspExceptionHandler : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext currentContext = application.Context;
// Gather information5
Exception currentException = application.Server.GetLastError();
String errorPage = "http://www.mycompaniesmainsite.com/error.html";
HttpException httpException = currentException as HttpException;
if (httpException == null || httpException.GetHttpCode() != 404)
{
currentContext.Server.Transfer(errorPage, true);
}
else
{
application.Response.Status = "404 Not Found";
application.Response.StatusCode = 404;
application.Response.StatusDescription = "Not Found";
currentContext.Server.Transfer(errorPage, true);
}
}
}
}
可能有人请向我解释什么,我做错了,以及如何IIS 7的集成管理管道模式有效吗?因为我发现的大部分答案都是关于为IIS 6配置HttpModules
。
感谢您的响应速度快,我没有设置应用程序池管理的管道模式。我在本地机器上使用IIS Express试用了它,当这种情况不起作用时,我将该站点发布到了IIS 7的服务器。在那里,我可以看到模块正在加载,但它永远不会被解雇。所以我正在做其他事情,我做错了... – 2011-05-10 06:41:45
在我的web.config和一些干净/生成解决方案的语法错误,并在稍后发布,它似乎工作... – 2011-05-11 13:13:14
@jonas - 不错的一个:) – Kev 2011-05-11 13:14:20