我的解决方案 - 除非有人提出一个更优雅的 - 是修改WebFormRouting项目中的WebFormRouteHandler类以接受自定义谓词。
public WebFormRouteHandler(string virtualPath, bool checkPhysicalUrlAccess, Func<RequestContext, string> custom)
然后在类内部,我将自定义参数存储到私有的CustomVirtualPath属性中。要使用它,我不得不GetSubstitutedVirtualPath改成这样:
public string GetSubstitutedVirtualPath(RequestContext requestContext)
{
string path = VirtualPath;
if (CustomVirtualPath != null)
{
path = CustomVirtualPath(requestContext);
}
if (!path.Contains("{")) return path;
//Trim off ~/
string virtualPath = path.Substring(2);
Route route = new Route(virtualPath, this);
VirtualPathData vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);
if (vpd == null) return path;
return "~/" + vpd.VirtualPath;
}
该项目要编译我们需要改变WebFormRoute和WebFormRouteExtensions允许自定义参数环比下跌的传递。当所有做我可以的global.asax.cs
routes.MapWebFormRoute("All", "{any}.aspx", "~/", false,
context =>
{
return ((string)context.RouteData.Values["any"] == "test"
? "~/PageProcessor.aspx"
: "~/DifferentPageProcessor.aspx");
});
当然lambda表达式应该查找从别的地方(数据库或缓存)的URL的身体写这篇文章。
来源
2009-05-20 21:58:42
pbz