简体   繁体   English

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

[英]Process html files with HttpModule to catch 404 errors on IIS7

I am having another problem with my HttpModule that handles exceptions. 我的HttpModule处理异常时遇到了另一个问题。 (cfr. my previous post: Custom HttpModule for IIS 7 for integrated ) (cfr。我以前的帖子: 用于集成的IIS 7的自定义HttpModule

All works well, but only for aspx pages. 一切正常,但仅适用于aspx页面。

The main reason we wanted to use this HttpModule is to handle 404 exceptions that occur when someone tries to go to a html page that doesn't exist. 我们想要使用这个HttpModule的主要原因是处理当有人试图转到不存在的html页面时发生的404异常。 But my HttpModule only works for .aspx pages and it isn't triggered when a html file doesn't exist. 但我的HttpModule仅适用于.aspx页面,并且当html文件不存在时不会触发。

This is the configuration that I have set up in my web.conf file: 这是我在web.conf文件中设置的配置:

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

I have also tried adding 'runAllManagedModulesForAllRequests="true"' to the module node, and 'preCondition="managedHandler"' to the "add" node, but this also didn't work. 我还尝试将'runAllManagedModulesForAllRequests =“true”'添加到模块节点,并将'preCondition =“managedHandler”'添加到“添加”节点,但这也不起作用。

I have set the Application pool on which my web application is running to "Integrated" mode, because I found this a lot on google. 我已将运行我的Web应用程序的应用程序池设置为“集成”模式,因为我在谷歌上发现了很多。

Is there another way to make my HttpModule handle exceptions that occur when visiting a non existing html page? 还有另一种方法可以让我的HttpModule处理访问非现有html页面时发生的异常吗?

Thanks! 谢谢!

Having done a little research based on the answer of Alexander, I implemented IHttpHandler in my AspExceptionHandler as well. 根据Alexander的答案做了一点研究,我在AspExceptionHandler中实现了IHttpHandler。

My class now looks like: 我的课现在看起来像:

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);
            }
        }
    }

In the ProcessRequest method (required by the IHttpHandler) I check if the file exists. 在ProcessRequest方法(IHttpHandler所需)中,我检查文件是否存在。 If it does not exist, I throw an HttpException that is catched by the HttpModule part of my class. 如果它不存在,我抛出一个由我的类的HttpModule部分捕获的HttpException。

The system.webServer node in my web.config now looks like this: 我的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>

I found the answer in in this post: HttpHandler fire only if file doesn't exist 我在这篇文章中找到了答案: 仅当文件不存在时才会触发HttpHandler

you can try this 你可以试试这个

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

this help you to catch all request, but if reqest is made for existing page you need to pass it to standart handler 这有助于您捕获所有请求,但如果对现有页面进行了reqest,则需要将其传递给标准处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM