ASP.NET 路由 - 如果路径存在,GetRouteData 不起作用
我有一个 HttpModule ,它拦截所有请求并根据路由规则从数据库加载数据。然而,我总是遇到一个问题; GetRouteData
仅在路径不存在时有效:
var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
假设请求传入 url http://localhost/contact
,我会获得与该 url 相关的正确路由数据如果文件系统中不存在该路径。当我想要自定义该 url 的页面时,就会出现问题,我是通过在路径 ~/contact/default.aspx
中创建一个 aspx 页面来实现的。执行此操作后,GetRouteData
返回 null
。
我什至尝试创建一个新的 HttpContext 对象,但如果页面存在,我仍然无法检索路由数据。
有人遇到过这个问题吗?有解决方案/解决方法吗?
我们将不胜感激所有帮助。
I have a HttpModule
which intercepts all requests and loads data from the database based on routing rules. However, I run into one problem all the time; GetRouteData
only works if the path does not exist:
var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
Assuming a request comes in for the url http://localhost/contact
, I get the correct routing data relating to that url if that path does not exist in the file system. The problem appears when I want to customize the page at that url which I do by creating an aspx page in the path ~/contact/default.aspx
. Once I do that, GetRouteData
return null
.
I have even tried creating a new HttpContext
object, but I still can not retrieve route data if the page exists.
Has anyone ever run into this problem? Is there a solution/workaround?
All help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 RouteCollection.RouteExistingFiles 设置为 true。
不过要小心。在 Visual Studio 中进行调试时,IIS7 的行为与使用的服务器略有不同。当我将应用程序部署到网络上时,我遇到了这个问题。看看这个我提交给 Microsoft Connection 的反馈。
Set
RouteCollection.RouteExistingFiles
to true.Beware though. IIS7 behaves a little differently than the server used when debugging within Visual Studio. I got bit by this when I deployed my application to the web. Check out this feedback I submitted to Microsoft Connection.