gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:25:58 28 4
gpt4 key购买 nike

我的 HttpModule 处理异常时遇到另一个问题。 (参见我之前的帖子: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 页面时发生的异常吗?

谢谢!

最佳答案

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

我的类(class)现在看起来像:

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,它会被我类的 HttpModule 部分捕获。

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

关于c# - 使用 HttpModule 处理 html 文件以捕获 IIS7 上的 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966613/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com