/// <summary> /// Construtor padrão. /// </summary> /// <param name="name"></param> /// <param name="parent"></param> /// <param name="info"></param> public RouteInfoNavigate(string name, RouteInfoNavigate parent, RouteInfo info) { _uniqueID = Guid.NewGuid(); _name = name; _parent = parent; _info = info; }
/// <summary> /// Construtor padrão. /// </summary> /// <param name="virtualPath">Caminho virtual que o resultado representa.</param> /// <param name="info"></param> /// <param name="complexValues"></param> public LoadRouteResult(string virtualPath, RouteInfoNavigate info, List <KeyValuePair <int, string> > complexValues) { VirtualPath = virtualPath; Navigate = info; ComplexValues = complexValues; }
/// <summary> /// Carrega uma informação de route com base no virtual path informado. /// </summary> /// <param name="virtualPath"></param> /// <returns></returns> private static LoadRouteResult LoadRoute(string virtualPath) { try { string loc = ""; if (!string.IsNullOrEmpty(virtualPath) && virtualPath[0] == '~') { loc = virtualPath.Substring(1); } else { loc = virtualPath; } if (!string.IsNullOrEmpty(loc) && loc[0] == '/') { loc = loc.Substring(1); } loc = loc.ToLower(); if (RouteSettings.IgnoreRoutes.Count > 0 && RouteSettings.IgnoreRoutes.Exists(f => f.Length <= loc.Length && loc.IndexOf(f, StringComparison.InvariantCultureIgnoreCase) == 0)) { return(null); } RouteInfoNavigate routeNavInfo = null; string findKey = null; foreach (var i in RouteSettings.RoutesNavigate.Keys) { if (loc.Length >= i.Length && loc.IndexOf(i, 0, i.Length, StringComparison.InvariantCultureIgnoreCase) == 0 && (findKey == null || i.Length > findKey.Length)) { findKey = i; } } if (!string.IsNullOrEmpty(findKey)) { routeNavInfo = (RouteInfoNavigate)RouteSettings.RoutesNavigate[findKey]; } Match findMatch = null; RouteInfoNavigate findComplexNav = null; lock (RouteSettings.ComplexRoutesNavigate) foreach (KeyValuePair <string, RouteInfoNavigate> i in RouteSettings.ComplexRoutesNavigate) { var m = Regex.Match(loc, i.Key, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); if (m.Success && m.Index == 0) { findComplexNav = i.Value; findMatch = m; } } if (routeNavInfo != null && findComplexNav != null) { var complexUrl = findComplexNav.Info.Location; var routeLocation = routeNavInfo.Info.Location; for (int i = 0; i < findComplexNav.ComplexPartsCount; i++) { complexUrl = complexUrl.Replace("{" + i + "}", findMatch.Groups["complex" + i].Value); } if (routeLocation.Length > 0 && routeLocation[routeLocation.Length - 1] != '/') { routeLocation += '/'; } if (complexUrl.Length > 0 && complexUrl[complexUrl.Length - 1] != '/') { complexUrl += '/'; } if (complexUrl.Length < routeNavInfo.Info.Location.Length || string.Compare(complexUrl, routeLocation, true) == 0) { return(new LoadRouteResult(virtualPath, routeNavInfo, null)); } } if (findComplexNav != null) { var groupValues = new List <KeyValuePair <int, string> >(); if (findMatch != null) { for (int i = 0; i < findComplexNav.ComplexPartsCount; i++) { groupValues.Add(new KeyValuePair <int, string>(i, findMatch.Groups["complex" + i].Value)); } } return(new LoadRouteResult(virtualPath, findComplexNav, groupValues)); } if (routeNavInfo != null) { return(new LoadRouteResult(virtualPath, routeNavInfo, null)); } } catch (Exception ex1) { throw new Exception(string.Format("Fail on process route to \"{0}\".", virtualPath), ex1); } return(null); }
public static void AddRouteInfo(string name, string location, string destinationPage, string path, string compiled, string actionName, string isDirectory, string isSecurePage, string forceNoSecurePage) { if (string.IsNullOrEmpty(path)) { path = "*"; } else { path = path.Replace(".", "").Replace(" ", ""); } bool isCompiled = true; if (!bool.TryParse(compiled, out isCompiled)) { isCompiled = true; } bool isDirectory1 = false; if (!bool.TryParse(isDirectory, out isDirectory1)) { isDirectory1 = false; } bool isSecurePage1 = false; if (!bool.TryParse(isSecurePage, out isSecurePage1)) { isSecurePage1 = false; } bool forceNoSecurePage1 = false; if (!bool.TryParse(forceNoSecurePage, out forceNoSecurePage1)) { forceNoSecurePage1 = false; } RouteInfo info = new RouteInfo(name, location, destinationPage, path, isCompiled, actionName, isDirectory1, isSecurePage1, forceNoSecurePage1); string loc = ""; if (!string.IsNullOrEmpty(info.Location) && info.Location[0] == '~') { loc = info.Location.Substring(1); } else { loc = info.Location; } if (!string.IsNullOrEmpty(loc) && loc[0] == '/') { loc = loc.Substring(1); } loc = loc.ToLower(); var complexParts = Regex.Matches(loc, "{(?<part>[0-9]*?)}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); if (complexParts.Count > 0) { var complexRegex = loc; if (complexParts.Count == 1 && complexParts[0].Index == 0 && complexParts[0].Length == complexRegex.Length) { complexRegex = "((?<complex" + complexParts[0].Groups["part"].Value + ">[\\s\\S]*?)/)|(?<complex" + complexParts[0].Groups["part"].Value + ">[\\s\\S]*)"; } else { foreach (Match i in complexParts) { if ((i.Index + i.Length == loc.Length)) { complexRegex = complexRegex.Replace(i.Value, "(((?<complex" + i.Groups["part"].Value + ">[\\s\\S]*?)/)|(?<complex" + i.Groups["part"].Value + ">[\\s\\S]*))"); } else { complexRegex = complexRegex.Replace(i.Value, "(?<complex" + i.Groups["part"].Value + ">[\\s\\S]*?)"); } } } if (!_complexRoutesNavigate.ContainsKey(complexRegex)) { RouteInfoNavigate rNavAux = new RouteInfoNavigate(loc, null, info); rNavAux.ComplexPartsCount = complexParts.Count; rNavAux.ComplexRegex = complexRegex; _complexRoutesNavigate.Add(complexRegex, rNavAux); } } else if (!_routesNavigate.ContainsKey(loc)) { RouteInfoNavigate rNavAux = new RouteInfoNavigate(loc, null, info); _routesNavigate.Add(loc, rNavAux); } }
private void PostResolveRequestCache(HttpContext context) { System.Diagnostics.Debug.Write("\r\n PostResolveRequestCache -- " + context.Request.Path); var cacheItem = context.Items[_requestCacheItemKey] as LoadRouteResult; RouteInfoNavigate nav = cacheItem != null ? cacheItem.Navigate : null; if (nav != null) { string relative = context.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + context.Request.PathInfo; var destinationPage = nav.Info.DestinationPage; string[] partsRelative = relative.Split('/'); string[] partsNav = nav.FullName.Split('/'); string[] extensions = nav.Info.Path.Split(','); string pageName = null; string pageExtension = null; string part = partsRelative[partsRelative.Length - 1]; if (string.IsNullOrEmpty(part)) { if (Array.FindIndex(extensions, delegate(string s) { return(s == "*"); }) < 0) { return; } pageName = ""; } else { System.IO.FileInfo fi = new System.IO.FileInfo(partsRelative[partsRelative.Length - 1]); pageExtension = fi.Extension; if (partsRelative[partsRelative.Length - 1].IndexOf('.') >= 0) { int i = 0; if (extensions.Length >= 1 && extensions[0] != "*") { for (i = 0; i < extensions.Length; i++) { if (string.Compare(fi.Extension.Substring(1), extensions[i].Trim(), true) == 0) { break; } } if (i == extensions.Length) { return; } } if (fi.Extension.Length > 0) { pageName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length); } else { pageName = fi.Name; } } else { int i = 0; for (i = 0; i < extensions.Length; i++) { if (extensions[i].Trim() == "*") { break; } } if (i == extensions.Length) { return; } pageName = fi.Name; } } var routeParameters = new System.Collections.Specialized.NameValueCollection(); int count = (pageName == null ? partsRelative.Length : (partsRelative.Length - 1)); for (int i = partsNav.Length; i < count; i++) { routeParameters.Add("param" + (i - partsNav.Length), partsRelative[i]); } if (pageName != null) { routeParameters.Add("pageName", pageName); } if (pageExtension != null) { routeParameters.Add("pageExtension", pageExtension); } if (nav.ComplexPartsCount > 0) { try { var match = System.Text.RegularExpressions.Regex.Match(relative, nav.ComplexRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.ExplicitCapture); if (match.Success) { for (int i = 0; i < nav.ComplexPartsCount; i++) { routeParameters.Add("complex" + i, match.Groups["complex" + i].Value); } } } catch (Exception ex) { throw new Exception(string.Format("Fail on process complex route for {0}.", relative), ex); } } var originalQueryStringParameters = new System.Collections.Specialized.NameValueCollection(); foreach (var i in context.Request.QueryString.AllKeys) { originalQueryStringParameters.Add(i, context.Request.QueryString[i]); } var qsPos = destinationPage.IndexOf('?'); if (qsPos >= 0) { var queryStringItems = destinationPage.Substring(qsPos + 1).Split('&'); foreach (var i in queryStringItems) { var pos1 = i.IndexOf('='); if (pos1 > 0) { routeParameters.Add(i.Substring(0, pos1), i.Substring(pos1 + 1)); } } destinationPage = destinationPage.Substring(0, qsPos); } var virtualPath = ""; RouteActionResponse actionResponse = null; var isCompiled = nav.Info.Compiled; var startPos = 0; if (nav.Info.IsDirectory) { var relativeDir = relative.Length > cacheItem.FormattedLocation.Length ? relative.Substring(cacheItem.FormattedLocation.Length) : relative; startPos = 0; if (!string.IsNullOrEmpty(relativeDir)) { while (startPos < relativeDir.Length && relativeDir[startPos] == '/' || relativeDir[startPos] == '\\') { startPos++; } } destinationPage += (startPos > 0 ? relativeDir.Substring(startPos) : relativeDir); } startPos = 0; if (!string.IsNullOrEmpty(destinationPage)) { while (startPos < destinationPage.Length && destinationPage[startPos] == '/' || destinationPage[startPos] == '\\') { startPos++; } } destinationPage = (startPos > 0 ? destinationPage.Substring(startPos) : destinationPage); if (!isCompiled) { virtualPath = "~/" + destinationPage; actionResponse = nav.Info.PreLoad(new RouteActionRequest { DestinationVirtualPath = virtualPath, OriginalQueryString = originalQueryStringParameters, RouteParameters = routeParameters, Context = context }); if (actionResponse != null && !string.IsNullOrEmpty(actionResponse.RedirectVirtualPath)) { isCompiled = actionResponse.Compiled; virtualPath = actionResponse.RedirectVirtualPath; } } else { virtualPath = "~/" + destinationPage; actionResponse = nav.Info.PreLoad(new RouteActionRequest { DestinationVirtualPath = virtualPath, OriginalQueryString = originalQueryStringParameters, RouteParameters = routeParameters, Context = context }); if (actionResponse != null && !string.IsNullOrEmpty(actionResponse.RedirectVirtualPath)) { isCompiled = actionResponse.Compiled; virtualPath = actionResponse.RedirectVirtualPath; } } IHttpHandler ret = null; var inputFile = context.Server.MapPath(virtualPath); if (actionResponse != null && actionResponse.RouteHandler != null) { ret = actionResponse.RouteHandler; } else if (!isCompiled) { ret = new NotCompiledRedirect { Filename = inputFile } } ; else if (string.Compare(System.IO.Path.GetExtension(inputFile), ".asmx", true) == 0) { ret = (new System.Web.Services.Protocols.WebServiceHandlerFactory()).GetHandler(context, "*", virtualPath, inputFile); } else { ret = PageParser.GetCompiledPageInstance(virtualPath, inputFile, context); } RequestData data2 = new RequestData(); data2.OriginalPath = context.Request.Path; data2.OriginalQueryStringParameters = originalQueryStringParameters; data2.HttpHandler = ret; data2.RouteParameters = routeParameters; data2.Route = nav.Info; context.Items[_requestDataKey] = data2; context.RewritePath("~/Route.axd"); } }