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

问题描述

我使用 URL路由的WebForms和我想确定这是一个路线动态的。比方说,我有这样的路线:

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:

{}任何的.aspx - 去 - >〜/ PageProcessor.aspx

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

这将需要这不是给PageProcessor页物理页的任何请求。这个伟大的工程。问题在于,基于来自数据库的一些数据,我需要某些网页被路由到不同的处理器,让我们说DifferentPageProcessor.aspx。因为第一个捕捉一切,我无法定义捕捉所有的.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.

所以,我需要一种方法来处理请求之前,路由器决定把它带到PageProcessor并根据需要它叉要么PageProcessor或DifferentPageProcessor。这可能吗?

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?

感谢。

推荐答案

我的解决方案 - 除非有人来了一个更优雅的一项 - 是修改WebFormRouteHandler类的WebFormRouting项目接受定制predicate

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)

然后在类中我会存储自定义参数为民营CustomVirtualPath财产。要使用它,我不得不改变GetSubstitutedVirtualPath这样:

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

有关项目进行编译,我们需要改变WebFormRoute和WebFormRouteExtensions允许自定义参数环比下跌的传递。当所有做过我可以写这的global.asax.cs

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

当然拉姆达前pression的机构应查找URL从别的地方(数据库或缓存)。

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

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

08-12 15:45