在.NET 4.0中使用HttpHandler处理404时,如何确定当前路径?
我的应用程序使用 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
没有给我提供其他看起来像我可以使用的属性 - 像 RawUrl
和 Url
之类的东西都包含查询字符串和其他我认为的垃圾不需要。我只想要当前路径。
我该如何解决这个问题?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论