gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:14:05 25 4
gpt4 key购买 nike

我正在尝试编写一个自定义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com