与HTTP模块处理HTML文件捉对IIS7

与HTTP模块处理HTML文件捉对IIS7

本文介绍了与HTTP模块处理HTML文件捉对IIS7 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的HttpModule另一个问题是处理异常。 (CFR我以前的帖子: )

I am having another problem with my HttpModule that handles exceptions. (cfr. my previous post: Custom HttpModule for IIS 7 for integrated)

一切运作良好,但仅限于aspx页面。

All works well, but only for aspx pages.

主要的原因,我们希望使用该HTTP模块是处理当有人试图去一个不存在的HTML页面时发生异常404。可是我的HttpModule只适用于.aspx页,当一个HTML文件不存在,它不会被触发。

这是我的配置成立于我的web.conf文件:

This is the configuration that I have set up in my web.conf file:

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



我也尝试添加'runAllManagedModulesForAllRequests =真到模块节点,前提= managedHandler来增加的节点,但是这也没有工作。

I have also tried adding 'runAllManagedModulesForAllRequests="true"' to the module node, and 'preCondition="managedHandler"' to the "add" node, but this also didn't work.

我已经设置了应用程序池上我的web应用程序运行于综合模式,因为我发现这个有很多关于谷歌。

I have set the Application pool on which my web application is running to "Integrated" mode, because I found this a lot on google.

有另一种方式让我的HttpModule搞定参观时发生的异常不存在的的 HTML 的网页?

Is there another way to make my HttpModule handle exceptions that occur when visiting a non existing html page?

谢谢!

推荐答案

做完根据亚历山大的回答一个小小的研究,我在AspExceptionHandler实施的IHttpHandler以及

Having done a little research based on the answer of Alexander, I implemented IHttpHandler in my AspExceptionHandler as well.

我班现在看起来像:

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。

In the ProcessRequest method (required by the IHttpHandler) I check if the file exists.If it does not exist, I throw an HttpException that is catched by the HttpModule part of my class.

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

The system.webServer node in my web.config now looks like this:

<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>



我在这篇文章找到了答案:的

这篇关于与HTTP模块处理HTML文件捉对IIS7 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:45