我有一个HttpModule
,它拦截所有请求并根据路由规则从数据库加载数据。然而,我总是遇到一个问题; GetRouteData
仅在路径不存在时有效:
var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
假设请求传入 URL http://localhost/contact
,如果文件系统中不存在该路径,我将获得与该 URL 相关的正确路由数据。当我想要自定义该 url 的页面时,就会出现问题,我是通过在路径 ~/contact/default.aspx
中创建一个 aspx 页面来实现的。执行此操作后,GetRouteData
返回 null
。
我什至尝试创建一个新的 HttpContext
对象,但如果页面存在,我仍然无法检索路由数据。
有人遇到过这个问题吗?有解决方案/解决方法吗?
我们将不胜感激所有帮助。
最佳答案
将RouteCollection.RouteExistingFiles
设置为true。
public static void RegisterRoutes(RouteCollection routes)
{
// Cause paths to be routed even if they exists physically
routes.RouteExistingFiles = true;
// Map routes
routes.MapPageRoute("...", "...", "...");
}
不过要小心。在 Visual Studio 中进行调试时,IIS7 的行为与使用的服务器略有不同。当我将应用程序部署到网络上时,我遇到了这个问题。看看这个feedback I submitted to Microsoft Connection .
关于ASP.NET 路由 - 如果路径存在,GetRouteData 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5852266/