WebForms 自定义/动态路由 [英] WebForms custom / dynamic routing

查看:25
本文介绍了WebForms 自定义/动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Phil Haack 的 WebForms 的 URL 路由,我想定义一个动态"路由.假设我有这条路线:

"{any}.aspx" -- 转到 --> "~/PageProcessor.aspx"

这会将任何非物理页面的请求带到 PageProcessor 页面.这很好用.问题是,基于一些来自数据库的数据,我需要将某些页面路由到不同的处理器,比如 DifferentPageProcessor.aspx.我无法定义捕获所有 .aspx 文件的新路由,因为第一个路由捕获所有内容.

因此,在路由器"决定将请​​求带到 PageProcessor 并根据需要将其分叉到 PageProcessor 或 DifferentPageProcessor 之前,我需要一种方法来处理请求.这可能吗?

谢谢.

解决方案

我的解决方案——除非有人提出更优雅的解决方案——是修改 WebFormRouting 项目中的 WebFormRouteHandler 类以接受自定义谓词.

>

public WebFormRouteHandler(string virtualPath, bool checkPhysicalUrlAccess, Func custom)

然后在类中,我将自定义参数存储到私有 CustomVirtualPath 属性中.要使用它,我必须将 GetSubstitutedVirtualPath 更改为:

public string GetSubstitutedVirtualPath(RequestContext requestContext){字符串路径 = VirtualPath;if (CustomVirtualPath != null){路径 = CustomVirtualPath(requestContext);}if (!path.Contains("{")) 返回路径;//修剪掉~/string virtualPath = path.Substring(2);Route route = new Route(virtualPath, this);VirtualPathData vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);if (vpd == null) 返回路径;返回 "~/" + vpd.VirtualPath;}

为了编译项目,我们需要更改 WebFormRoute 和 WebFormRouteExtensions 以允许自定义参数沿链向下传递.完成后我可以在 global.asax.cs 中写这个

routes.MapWebFormRoute("All", "{any}.aspx", "~/", false,上下文 =>{return ((string)context.RouteData.Values["any"] == "test"?~/PageProcessor.aspx": "~/DifferentPageProcessor.aspx");});

当然,lambda 表达式的主体应该从其他地方(数据库或缓存)查找 URL.

I'm using Phil Haack's URL routing for WebForms and I would like to define a route that's "dynamic." Let's say I have this route:

"{any}.aspx" -- goes to --> "~/PageProcessor.aspx"

This would take any request that's not a physical page to the PageProcessor page. This works great. The problem is that, based on some data that comes from a database, I need certain pages to be routed to a different processor, let's say DifferentPageProcessor.aspx. I can't define a new route that catches all the .aspx files because the first one catches everything.

So, I would need a way to process the request before the "router" decides to take it to PageProcessor and fork it to either PageProcessor or DifferentPageProcessor as needed. Is this possible?

Thanks.

解决方案

My solution -- unless somebody comes up with a more elegant one -- was to modify the WebFormRouteHandler class in the WebFormRouting project to accept a custom predicate.

public WebFormRouteHandler(string virtualPath, bool checkPhysicalUrlAccess, Func<RequestContext, string> custom)

Then inside the class I would store the custom parameter into private CustomVirtualPath property. To use it, I had to change GetSubstitutedVirtualPath to this:

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;
}

For the project to compile we need to change WebFormRoute and WebFormRouteExtensions to allow the passing of the custom parameter down the chain. When all done I can write this in global.asax.cs

routes.MapWebFormRoute("All", "{any}.aspx", "~/", false,
                         context =>
                           {
                             return ((string)context.RouteData.Values["any"] == "test"
                                       ? "~/PageProcessor.aspx"
                                       : "~/DifferentPageProcessor.aspx");
                           });

Of course the body of the lambda expression should look up the URL from some other place (database or cache).

这篇关于WebForms 自定义/动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆