加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 用于集成的IIS 7的自定义HttpModule

发布时间:2020-12-16 04:23:16 所属栏目:asp.Net 来源:网络整理
导读:我遇到了我构建的自定义错误处理程序的麻烦.它应该是一个HttpModule,但当我将它添加到我的web.config的system.webServer / modules标签时,它不会被启动. 这是我的web.config部分: system.webServer modules add name="AspExceptionHandler" type="Company.E
我遇到了我构建的自定义错误处理程序的麻烦.它应该是一个HttpModule,但当我将它添加到我的web.config的system.webServer / modules标签时,它不会被启动.

这是我的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配置HttpModule.

解决方法

从我所看到的你走在正确的轨道上.您是否确定您的站点的应用程序池设置为Managed Pipeline模式?

此外,如果您使用内置的Visual Studio Web服务器(Cassini)进行测试,那么< system.webServer>部分将被忽略.如果您希望从那里加载模块,则需要IIS7或IIS7.5 Express.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读