asp.net - ASP.NET 未处理文件夹请求

标签 asp.net iis http-status-code-404 httphandler httpmodule

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

预先感谢您的所有帮助。

最佳答案

在 IIS 中,将默认 404 页面设置为指向您的处理程序。发生的情况是 IIS 在 404 进入 .net 工作进程之前就对其进行了处理。

关于asp.net - ASP.NET 未处理文件夹请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12698238/

相关文章:

asp.net-mvc - ASP.NET MVC : how to catch a 404 error?

c# - 提交后清除所有字段

asp.net - 使用 Python Mechanize 对 aspx 进行屏幕截图 - Javascript 表单提交

asp.net - 检查 HTTP 请求查询字符串并在出现意外参数时指示错误是个好主意吗?

macos - IIS 7.5 上带有 SSL(自签名)的 WebDAV - Windows 无法连接 - Mac 只能以只读方式挂载

http - Sitecore 使用 "302 found"将用户重定向到我的自定义 404 页面——这合法吗?

java - http 状态 404 – 在 Tomcat 服务器上的 jsp、servlet 中未找到

asp.net - Asp CheckBoxList 中的多个数据字段?

asp.net - Visual Studio 2019--来自 HRESULT 0x80041fe4 的异常

c# - 尝试访问 ASP.NET Web App 项目上的本地主机时出现 ERR_CONNECTION_RESET