Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpModule处理html文件,以捕获IIS7上的404错误_C#_Iis 7_Httpmodule - Fatal编程技术网

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

C# 使用HttpModule处理html文件,以捕获IIS7上的404错误,c#,iis-7,httpmodule,C#,Iis 7,Httpmodule,我处理异常的HttpModule还有另一个问题。(cfr.我以前的职位:) 这一切都很好,但只适用于aspx页面 我们希望使用这个HttpModule的主要原因是处理404个异常,当有人试图转到一个不存在的html页面时会发生这些异常。但是我的HttpModule只适用于.aspx页面,当html文件不存在时不会触发它 这是我在web.conf文件中设置的配置: <system.webServer> <modules> <add name="AspExc

我处理异常的HttpModule还有另一个问题。(cfr.我以前的职位:)

这一切都很好,但只适用于aspx页面

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

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

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

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

我已经将运行web应用程序的应用程序池设置为“集成”模式,因为我在google上发现了很多

是否有其他方法使我的HttpModule处理访问不存在的html页面时发生的异常?

谢谢

你可以试试这个

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

....

这有助于捕获所有请求,但如果对现有页面进行了request,则需要将其传递给standart handler

基于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所需)中,我检查文件是否存在。 如果它不存在,我抛出一个HttpException,该HttpException由类的HttpModule部分捕获

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


AspexExceptionHandler不是必须是IHttpHandler才能添加为处理程序吗?好的,我已经测试过了,我得到了一个错误AspExceptionHandler没有实现IHttpHandler,或者这就是您所说的,我应该将我的类转换为HttpHandler,以便它处理所有请求?对不起,我认为您的类是IHttpHandler,因为它的名称:)无论如何,我希望这个答案能告诉你怎么做现在,是的,我选择AspexExceptionHandler是因为它处理异常:)。你的帖子真的很有帮助,正如我在下面的回答中所看到的:)。。。非常感谢你的帮助+1用于在到期时给予信贷。这基本上是正确的,但有点愚蠢。为什么要创建一个模块和处理程序的类?您实际上只是在使用处理程序方面。此外,如果要将其注册为“*”(任何穿过裂缝的内容),它将超出其界限并停止其他处理程序的工作:)谢谢你的评论。