WebForms custom / dynamic routing

萝らか妹 提交于 2019-12-01 01:58:58

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).

Markive

It's in VB but this is how I do it:

 Routes.Add("Root", New Route("{*URLRequest}", New myRootRouteHandler()))

This does the same as yours, it catches the entire request but it then passes it off to my own custom route handler class:

Imports System.Web.Routing
Imports System.Security
Imports System.Web.Compilation
Imports System.Web

Public Class myRootRouteHandler
  Implements IRouteHandler
  Implements IRequiresSessionState

  Private Path As String

  Public Function GetHttpHandler(ByVal requestContext As RequestContext) As IHttpHandler Implements IRouteHandler.GetHttpHandler
    Dim url As String = Web.HttpContext.Current.Request.ServerVariables("SERVER_NAME")
    Dim Key As String = requestContext.RouteData.Values("URLRequest")
    Dim myConfig As New LoadMyConfig(url)
    Dim aspxToLoad As String = myConfig.getPageVirtualPath(Key)
    Dim page As myCustom_Page_Base = TryCast(BuildManager.CreateInstanceFromVirtualPath(aspxToLoad, GetType(myCustom_Page_Base)), myCustom_Page_Base)

    page.RequestContext = requestContext
    page.strRequestContext = Key
    page.mySettings = myConfig

    Return page

End Function

In my example I have overloaded System.ui.page so that every aspx page in my project can inherit certain properties automatically like for example the mySettings object which contains my apps settings.. This is why I dim page as myCustom_Page_Base, you can load it as a standard page.

The important bit is: BuildManager.CreateInstanceFromVirtualPath method this will load whatever aspx file you point to it..

Hope this helps..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!