【问题标题】:Folder requests not being handled by ASP.NETASP.NET 不处理文件夹请求
【发布时间】:2012-09-23 18:37:02
【问题描述】:

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

提前感谢您的所有帮助。

【问题讨论】:

  • 另外,我很好奇您为什么不为此使用 Web.config 的 customErrors 部分。
  • 日志记录以及 customErrors 出于某种原因在通过身份验证后无法正常工作的事实,即它为任何 404 提供标准的“/应用程序中的服务器错误”页面。我想处理这个问题会更简单,但这是一个很好的学习体验:)

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


【解决方案1】:

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

【讨论】:

  • 我该怎么做?我只是让它指向 404.aspx 吗?或者我还有什么需要做的吗?
  • 打开 IIS。找到您的网站(应用程序),然后打开错误页面功能。会有一个 404 错误代码的设置,只需打开它并让它指向一个绝对 URL。就让它 /404.aspx
  • 好奇,您是否禁用了 ,因为您使用的是其他方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 2012-09-28
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 2011-05-21
相关资源
最近更新 更多