private static UrlMappingItem getUrlInfo(XmlNode node, NavigationItem menuItem, int index, int subIndex, int subsubIndex, IncomingQueryStringBehavior incomingQueryStringBehavior) { string name = XmlUtil.GetStringAttribute(node, "name", string.Empty); string urlTemplate = XmlUtil.GetStringAttribute(node, "template", string.Empty); if (string.IsNullOrEmpty(urlTemplate)) { throw new UrlMappingException("There is an XmlUrlMappingModule error. All <url> tags in the mapping file require a 'template' attribute."); } string redirection = Utility.GetHref(XmlUtil.GetStringAttribute(node, "href", string.Empty)); // still here, we can create the item and add to the collection UrlMappingItem item = Utility.CreateTemplatedMappingItem( name, urlTemplate, redirection, incomingQueryStringBehavior ); item.UrlTemplate = urlTemplate; // set custom attributes foreach (XmlAttribute attr in node.Attributes) { item[attr.Name] = attr.Value; } if (XmlUtil.GetStringAttribute(node, "index", string.Empty) == "?") { item.Index = null; } else { item.Index = index; } if (XmlUtil.GetStringAttribute(node, "subindex", string.Empty) == "?") { item.SubIndex = null; } else { item.SubIndex = subIndex; } if (XmlUtil.GetStringAttribute(node, "subsubindex", string.Empty) == "?") { item.SubsubIndex = null; } else { item.SubsubIndex = subsubIndex; } item.Title = XmlUtil.GetStringAttribute(node, "title", menuItem.Title); item.Id = XmlUtil.GetStringAttribute(node, "id", null); item.Action = XmlUtil.GetStringAttribute(node, "action", null); return(item); }
public NameValueCollection GetMappedQueryString(string urlRequested, out UrlMappingItem mapping) { mapping = null; urlRequested = (_qsBehavior == IncomingQueryStringBehavior.Include ? new Url(urlRequested).PathAndQuery : new Url(urlRequested).Path); urlRequested = GetUrlRequested(urlRequested); foreach (UrlMappingItem item in _provider.UrlMappings ?? new UrlMappingItemCollection()) { Match match = item.UrlTarget.Match(urlRequested); if (match.Success) { // do we want to add querystring parameters for dynamic mappings? NameValueCollection qs = new NameValueCollection(); if (match.Groups.Count > 1) { for (int i = 1; i < match.Groups.Count; i++) { qs.Add(item.UrlTarget.GroupNameFromNumber(i), match.Groups[i].Value); } } mapping = item; return(qs); } } return(new NameValueCollection()); }
/// <summary> /// Creates a <see cref="UrlMappingItem" /> given a name, templated URL string, and redirection string. /// </summary> /// <param name="name">A name for this mapping item</param> /// <param name="templatedUrl">A template for URLs to be matched</param> /// <param name="redirection">The redirection that the UrlMappingModule should apply when incoming URLs match the template.</param> /// <param name="qsBehavior">defines how the UrlMappingModule should treat querystring values on an incoming URL for pattern matching; this is typically provided through declarative configuration</param> /// <returns>The created <see cref="UrlMappingItem" /></returns> /// <remarks> /// <para> /// The template URL string is relative to the web application and may or may not include /// the "~/" characters at the beginning. The template may indicate an exact URL to match, /// for example <i>Reports/Summary.aspx</i> or may include token names surrounded /// by [SquareBrackets] for dynamic matching, such as <i>Reports/[ID]/[Action].aspx</i>. /// When dynamic templates are matched in runtime by the UrlMappingModule, dynamic tokens /// are appended to the redirection string as querystring items, /// such as <i>?ID=<i>xxx</i>&Action=<i>xxx</i></i>. /// </para> /// <para> /// The redirection string may be an absolute URL, such as <i>http://www.microsoft.com</i>, /// a server-relative URL beginning with a "/", such as <i>/AnotherAppOnThisServer/page.aspx</i>, /// or an application-relative URL to a concrete resource, optionally beginning with a "~/", such as /// <i>~/ReportSummary.aspx</i> or <i>ReportSummary.aspx</i>. /// </para> /// </remarks> public static UrlMappingItem CreateTemplatedMappingItem(string name, string templatedUrl, string redirection, IncomingQueryStringBehavior qsBehavior) { UrlMappingItem item = new UrlMappingItem(name, CreateTemplatedMappingRegex(templatedUrl, qsBehavior), redirection); item.UrlTemplate = templatedUrl; return(item); }
private bool Match(HttpRequest request, string urlRequested, out string newPath, out NameValueCollection qs) { qs = new NameValueCollection(); JContext jc = JContext.Current; UrlMappingItem matched = null; foreach (UrlMappingItem item in _provider.UrlMappings ?? new UrlMappingItemCollection()) { Match match = item.UrlTarget.Match(urlRequested); if (match.Success) { // add querystring parameters for dynamic mappings for (int i = 1; i < match.Groups.Count; i++) { qs.Add(item.UrlTarget.GroupNameFromNumber(i), match.Groups[i].Value); } // temp use jc.QueryString.Add(qs); matched = item; break; } } if (matched != null) { if (jc.Navigation.Set(matched, urlRequested)) { OnUrlMatched(); newPath = matched.Redirection; return(true); } } newPath = string.Empty; return(false); }
public static void ParseXml(string file, UrlMappingItemCollection routes, Dictionary <int, NavigationItem> menuItems, IncomingQueryStringBehavior incomingQueryStringBehavior) { if (!File.Exists(file)) { return; } XmlDocument xml = new XmlDocument(); try { xml.Load(file); } catch (Exception ex) { throw new UrlMappingException("The error occurred while loading the route files. A virtual path is required and the file must be well-formed.", ex); } menuItems.Clear(); int i = -1, j = -1, k = -1; foreach (XmlNode node in xml.DocumentElement.ChildNodes) { if (node.Name == "menu") { i++; NavigationItem menuItem = getMenuItem(node); menuItem.Children = new Dictionary <int, NavigationItem>(); menuItems[i] = menuItem; foreach (XmlNode subNode in node.ChildNodes) { if (subNode.Name == "menu") { j++; NavigationItem sub_menuItem = getMenuItem(subNode); sub_menuItem.Children = new Dictionary <int, NavigationItem>(); menuItems[i].Children[j] = sub_menuItem; foreach (XmlNode subsubNode in subNode.ChildNodes) { if (subsubNode.Name == "menu") { k++; NavigationItem subsub_menuItem = getMenuItem(subsubNode); sub_menuItem.Children[k] = subsub_menuItem; foreach (XmlNode last_node in subsubNode.ChildNodes) { if (last_node.Name == "url") { UrlMappingItem url = getUrlInfo(last_node, subsub_menuItem, i, j, k, incomingQueryStringBehavior); routes.Add(url); } } } else if (subsubNode.Name == "url") { UrlMappingItem url = getUrlInfo(subsubNode, sub_menuItem, i, j, -1, incomingQueryStringBehavior); routes.Add(url); } } } else if (subNode.Name == "url") { UrlMappingItem url = getUrlInfo(subNode, menuItem, i, -1, -1, incomingQueryStringBehavior); routes.Add(url); } } } else if (node.Name == "url") { UrlMappingItem url = getUrlInfo(node, new NavigationItem(), -1, -1, -1, incomingQueryStringBehavior); routes.Add(url); } } }
public void AddMapping(string siteKey, UrlMappingItem item) { _manualAdded.Add(item); _coll.Merge(item); }
public void AddMapping(UrlMappingItem item) { AddMapping(AreaConfig.Instance.AreaKey, item); }
public bool Set(UrlMapping.UrlMappingItem item, string requesturl) { Url = item; string url; if (item.Index == null || item.SubIndex == null || item.SubsubIndex == null) { Dictionary <int, NavigationItem> menuItems = UrlMapping.UrlMappingModule.Instance.Provider.MenuItems; if (item.Index == null) { double max = 0; int maxi = 0, maxj = 0, maxk = 0; foreach (int i in menuItems.Keys) { double d = 0; foreach (int j in menuItems[i].Children.Keys) { NavigationItem ni = menuItems[i].Children[j]; foreach (int k in ni.Children.Keys) { NavigationItem nii = ni.Children[k]; url = trimUrl(nii.Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxi = i; maxj = j; maxk = k; } } url = trimUrl(ni.Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxi = i; maxj = j; maxk = -1; } } url = trimUrl(menuItems[i].Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxi = i; maxj = -1; maxk = -1; } } if (max > 0) { Index = maxi; SubIndex = maxj; SubsubIndex = maxk; } else { return(false); } } else if (item.SubIndex == null) { Index = item.Index.Value; double max = 0; int maxj = 0, maxk = 0; double d = 0; foreach (int j in menuItems[Index].Children.Keys) { NavigationItem ni = menuItems[Index].Children[j]; foreach (int k in ni.Children.Keys) { NavigationItem nii = ni.Children[k]; url = trimUrl(nii.Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxj = j; maxk = k; } } url = trimUrl(ni.Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxj = j; maxk = -1; } } if (max > 0) { SubIndex = maxj; SubsubIndex = maxk; } else { return(false); } } else { double max = 0; int maxk = 0; double d = 0; NavigationItem ni = menuItems[Index].Children[SubIndex]; foreach (int k in ni.Children.Keys) { NavigationItem nii = ni.Children[k]; url = trimUrl(nii.Url); if (string.IsNullOrEmpty(url)) { continue; } d = StringUtil.Similarity(requesturl, url); if (d > max) { max = d; maxk = k; } } if (max > 0) { SubsubIndex = maxk; } else { return(false); } } } else { Index = item.Index.Value; SubIndex = item.SubIndex.Value; SubsubIndex = item.SubsubIndex.Value; } Title = item.Title; Name = item.Name; foreach (string key in Url.Keys) { SetExtendedAttribute(key, Url[key]); } OK = true; return(true); }