【发布时间】:2018-04-09 15:08:05
【问题描述】:
我想处理所有请求。 我无法处理带有扩展名的请求。让我告诉你我的意思。
1) 首先让我展示一下我的 Web 应用程序有什么:(非常简单)
Web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="Handle404.aspx">
<error statusCode="404" redirect="Handle404.aspx" />
</customErrors>
<compilation debug="true"/>
</system.web>
</configuration>
Global.asax
using System;
namespace DeleteMeWebAppliation
{
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
Console.Write("Test");
}
protected void Application_Error(object sender, EventArgs e)
{
Console.Write("Test");
}
}
}
Default.aspx
using System;
namespace DeleteMeWebAppliation
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("test");
}
}
}
Handle404.aspx
using System;
namespace DeleteMeWebAppliation
{
public partial class Handle404 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.Write("Test");
}
}
}
2) 现在问题来了:
注意我在Console.Write("Test") 的所有行上都有一个断点,我的目标是处理所有请求。 **换句话说,我想在每次向 IIS 发出请求时都打断点,而不管 url。
a) 当我转到 http://localhost:65284/ It Works(我遇到了断点)
b) 当我转到 http://localhost:65284/SomeNoneExistingPage.aspx It Works(我遇到了断点)
c) 当我转到 http://localhost:65284/Foooooo It Works(我遇到了断点)
d) 当我转到 http://localhost:65284/Foooooo/fooo/fooo/test.aspx It Works(我遇到了断点)
e) 当我转到http://localhost:65284/foo.test 失败(我没有断点)为什么 Application_Error 方法上的断点没有命中!?
e) 当我转到http://localhost:65284/foo/foo.jpg 失败(我没有断点)为什么 Application_Error 方法上的断点没有命中!?
总之,例如,当有人转到 http://localhost:65284/foo.test 时,我该如何执行我的自定义代码。一切都很好,但是当我转到一个带有“。”的网址时最后用其他东西而不是 aspx 然后它失败了!
互联网上有很多地方展示了如何处理 404 错误,但由于某种原因,我无法使其正常工作。
当我转到http://localhost:65284/foo.test 时,我得到了一个 404 错误,由于某种原因,我没有被重定向到 404 处理程序。如果链接以 aspx 结尾,我会被重定向。没有意义
【问题讨论】:
-
我运行的是 IIS 版本 10。当我将它发布到 IIS 服务器以及在 IIS Exrpress 上运行时,就会发生这种情况
-
网页浏览器在加载
http://localhost:65284/foo/foo.jpg时会显示什么? -
听起来你需要启用 RAMFAR。
标签: c# asp.net web-applications http-status-code-404 global-asax