[英]Process html files with HttpModule to catch 404 errors on IIS7
我的HttpModule處理異常時遇到了另一個問題。 (cfr。我以前的帖子: 用於集成的IIS 7的自定義HttpModule )
一切正常,但僅適用於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”'添加到“添加”節點,但這也不起作用。
我已將運行我的Web應用程序的應用程序池設置為“集成”模式,因為我在谷歌上發現了很多。
還有另一種方法可以讓我的HttpModule處理訪問非現有html頁面時發生的異常嗎?
謝謝!
根據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
你可以試試這個
<httpHandlers>
<add type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" verb="*" path="*"/>
....
</httpHandlers>
這有助於您捕獲所有請求,但如果對現有頁面進行了reqest,則需要將其傳遞給標准處理程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.