在.NET 4.0中使用HttpHandler处理404时,如何确定当前路径?

发布于 2025-01-02 03:20:19 字数 965 浏览 2 评论 0原文

我的应用程序使用 web.config 中的 HttpErrors 规则将 404 重定向到自定义 HttpHandler

<handlers>
  <add name="MyHandler" path="/default.aspx" verb="*" type="MyHandler.HttpHandler" />
</handlers>

<httpErrors errorMode="Custom">
  <remove statusCode="404" />
  <remove statusCode="405" />
  <error statusCode="404" path="/default.aspx" responseMode="ExecuteURL" />
  <error statusCode="405" path="/default.aspx" responseMode="ExecuteURL" />
</httpErrors>

在 .NET 2.0 中,这工作正常。现在我想将我的应用程序转换为 .NET 4.0,但它导致了问题。

问题是,当请求路由到 HttpHandler 时,我会检查 Request.Path 的值来确定我正在查看哪个页面。

在 .NET 2.0 中,Request.Path 将计算为 /page/subpage/item。在 .NET 4.0 中,它指的是 /default.aspx

检查 Request 没有给我提供其他看起来像我可以使用的属性 - 像 RawUrlUrl 之类的东西都包含查询字符串和其他我认为的垃圾不需要。我只想要当前路径。

我该如何解决这个问题?

My app uses a HttpErrors rule in the web.config to redirect 404s to a custom HttpHandler:

<handlers>
  <add name="MyHandler" path="/default.aspx" verb="*" type="MyHandler.HttpHandler" />
</handlers>

<httpErrors errorMode="Custom">
  <remove statusCode="404" />
  <remove statusCode="405" />
  <error statusCode="404" path="/default.aspx" responseMode="ExecuteURL" />
  <error statusCode="405" path="/default.aspx" responseMode="ExecuteURL" />
</httpErrors>

In .NET 2.0, this works fine. Now I want to convert my app to .NET 4.0, and it's causing problems.

The problem is that when a request is routed to the HttpHandler, I check the value of Request.Path to determine which page I'm looking at.

In .NET 2.0, the Request.Path would evaluate to i.e. /page/subpage/item. In .NET 4.0, it refers to /default.aspx.

Inspecting the Request gives me no other properties that look like something I could use - things like RawUrl and Url all include querystrings and other junk that I don't need. I just want the current path.

How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文