我试图编写自定义HttpHandler以处理我的所有404错误。处理程序正在捕获和处理我指定的所有文件类型但由于某种原因它不处理文件夹请求,即如果我放入mysite.com/foo/bar.html或mysite.com/ foo / bar.aspx它处理它并显示正确的错误页面,但如果我输入mysite.com/foo/它会显示一个完全空白的页面,没有源代码或任何东西。这是处理程序的代码:
public class RedirectHttpModule :IHttpHandler, IHttpModule {
public RedirectHttpModule() {
//
// TODO: Add constructor logic here
//
}
public void Dispose() { }
public void Init(HttpApplication context) {
context.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e) {
HttpApplication application = (HttpApplication)sender;
application.Context.Response.TrySkipIisCustomErrors = true;
Exception lastError = application.Server.GetLastError();
HttpException ex = lastError as HttpException;
ILog _logger = LogManager.GetLogger(typeof(Page));
string page = "~/404.aspx";
if (ex != null) {
application.Server.ClearError();
application.Context.Handler = System.Web.UI.PageParser.GetCompiledPageInstance(page, application.Server.MapPath(page), application.Context);
string username = application.Context.User.Identity.Name;
if (!String.IsNullOrEmpty(username)) _logger.ErrorFormat("HTTP Error {0}: {1} Username: {2}", ex.GetHttpCode().ToString(), ex.Message, username);
else _logger.ErrorFormat("HTTP Error {0}: {1}", ex.GetHttpCode().ToString(), ex.Message);
}
else {
application.Context.Handler = System.Web.UI.PageParser.GetCompiledPageInstance(page, application.Server.MapPath(page), application.Context);
}
}
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 or directory {0} does not exist.", context.Request.PhysicalPath));
}
else {
context.Response.TransmitFile(context.Request.PhysicalPath);
}
}
}
这里是Web.config的相关部分:
<handlers>
<add name="html-to-aspx-isapi" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="html-to-aspx" path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="htm-to-aspx-isapi" path="*.htm" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="htm-to-aspx" path="*.htm" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="asp-to-aspx-isapi" path="*.asp" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="asp-to-aspx" path="*.asp" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="RedirectHttpModule" modules="RedirectHttpModule" preCondition="" path="*" verb="*" resourceType="Either"/>
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<add name="RedirectHttpModule" type="RedirectHttpModule" preCondition="managedHandler"/>
</modules>
无论出于何种原因,即使它运行Integrated而不是Classic,如果我删除前6个处理程序,它将不再处理ASP.NET的html,htm或asp请求。我开始怀疑存在某种配置问题。有什么想法吗?
提前感谢您的帮助。
答案 0 :(得分:1)
在IIS中,将默认404页面设置为指向处理程序。发生的事情是IIS在它进入.net工作进程之前处理404。