使用HttpModule处理html文件以捕获IIS7上的404错误

时间:2011-05-11 15:18:50

标签: c# iis-7 httpmodule

我的HttpModule处理异常时遇到了另一个问题。 (cfr。我以前的帖子:Custom HttpModule for IIS 7 for integrated

一切正常,但仅适用于aspx页面。

我们想要使用这个HttpModule的主要原因是处理当有人试图转到不存在的html页面时发生的404异常。但我的HttpModule仅适用于.aspx页面,并且当html文件不存在时不会触发。

这是我在web.conf文件中设置的配置:

<system.webServer>
  <modules>
    <add name="AspExceptionHandler" 
         type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" 
         preCondition="managedHandler" />
  </modules>
</system.webServer>

我还尝试将'runAllManagedModulesForAllRequests =“true”'添加到模块节点,并将'preCondition =“managedHandler”'添加到“添加”节点,但这也不起作用。

我已将运行我的网络应用程序的应用程序池设置为“集成”模式,因为我在谷歌上发现了很多。

还有另一种方法可以让我的HttpModule处理访问不存在的 html 页面时发生的异常吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

根据Alexander的答案做了一点研究,我也在我的AspExceptionHandler中实现了IHttpHandler。

我的班级现在看起来像:

public class AspExceptionHandler : IHttpModule, IHttpHandler
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(ErrorHandler);
        }

        private void ErrorHandler(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            try
            {
                // Gather information
                Exception currentException = application.Server.GetLastError(); ;
                String errorPage = "http://companywebsite.be/error.aspx";

                HttpException httpException = currentException as HttpException;
                if (httpException == null || httpException.GetHttpCode() != 404)
                {
                    application.Server.Transfer(errorPage, true);
                }
                //The error is a 404
                else
                {
                    // Continue
                    application.Server.ClearError();

                    String shouldMail404 = true;

                    //Try and redirect to the proper page.
                    String requestedFile = application.Request.Url.AbsolutePath.Trim('/').Split('/').Last();

                    // Redirect if required
                    String redirectURL = getRedirectURL(requestedFile.Trim('/'));
                    if (!String.IsNullOrEmpty(redirectURL))
                    {
                        //Redirect to the proper URL
                    }
                    //If we can't redirect properly, we set the statusCode to 404.
                    else
                    {
                        //Report the 404
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionCatcher.FillWebException(HttpContext.Current, ref ex);
                ExceptionCatcher.CatchException(ex);
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (!File.Exists(context.Request.PhysicalPath)) 
            {
                throw new HttpException(404, String.Format("The file {0} does not exist", context.Request.PhysicalPath));
            }
                    else
            {
                context.Response.TransmitFile(context.Request.PhysicalPath);
            }
        }
    }

在ProcessRequest方法中(IHttpHandler需要)我检查文件是否存在。 如果它不存在,我抛出一个由我的类的HttpModule部分捕获的HttpException。

我的web.config中的system.webServer节点现在看起来像这样:

<modules runAllManagedModulesForAllRequests="true">
            <add name="AspExceptionHandler" type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" preCondition="managedHandler" />
        </modules>
        <handlers>
            <add name="AspExceptionHandler" type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" verb="*" path="*.html" />
        </handlers>

我在这篇文章中找到了答案:HttpHandler fire only if file doesn't exist

答案 1 :(得分:1)

你可以试试这个

<httpHandlers>
    <add type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" verb="*" path="*"/> 
    ....
</httpHandlers>

这有助于您捕获所有请求,但如果对现有页面进行了reqest,则需要将其传递给标准处理程序

相关问题