3

我正在尝试编写一个自定义 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 请求。我开始怀疑存在某种配置问题。有任何想法吗?

提前感谢您的所有帮助。

4

1 回答 1

1

In IIS, set the default 404 page to point to your handler. What's happening is that IIS is handling the 404 before it ever makes it to the .net worker process.

于 2012-10-02T21:06:14.673 回答