/// <summary> /// Gets the data parsed from the Route and QueryString of the <paramref name="context"/> /// with the controller and action Route defaults removed /// </summary> /// <param name="state">The <see cref="State"/> navigated to</param> /// <param name="context">The current context</param> /// <returns>The navigation data</returns> public override NameValueCollection GetNavigationData(State state, HttpContextBase context) { NameValueCollection data = base.GetNavigationData(state, context); data.Remove("controller"); data.Remove("action"); return data; }
/// <summary> /// Gets a link that navigates to the <paramref name="state"/> passing the <paramref name="data"/>. /// </summary> /// <param name="state">The <see cref="Navigation.State"/> to navigate to</param> /// <param name="data">The data to pass when navigating</param> /// <param name="context">The current context</param> /// <returns>The navigation link</returns> public override string GetNavigationLink(State state, NameValueCollection data, HttpContextBase context) { if (GetRouteName(state, context) != null) { return base.GetNavigationLink(state, data, context); } else { return GetLink(state, data, GetMobile(context), context.Request.ApplicationPath); } }
/// <summary> /// Redirects or Transfers to the <paramref name="url"/> depending on the /// <paramref name="mode"/> specified /// </summary> /// <param name="state">The <see cref="State"/> to navigate to</param> /// <param name="url">The target location</param> /// <param name="mode">Indicates whether to Redirect or Transfer</param> /// <param name="context">The current context</param> public virtual void NavigateLink(State state, string url, NavigationMode mode, HttpContextBase context) { if (mode != NavigationMode.Server) { if (!GetPermanent(state, context)) context.Response.Redirect(url, GetEndResponse(state, context)); else context.Response.RedirectPermanent(url, GetEndResponse(state, context)); } else context.Server.Transfer(url, GetPreserveForm(state, context)); }
/// <summary> /// Gets a routed link that navigates to the <paramref name="state"/> passing /// the <paramref name="data"/> /// </summary> /// <param name="state">The <see cref="Navigation.State"/> to navigate to</param> /// <param name="data">The data to pass when navigating</param> /// <param name="context">The current context</param> /// <returns>The routed navigation link</returns> public virtual string GetNavigationLink(State state, NameValueCollection data, HttpContextBase context) { RouteValueDictionary routeData = new RouteValueDictionary(); foreach (string key in data.Keys) { if (key != NavigationSettings.Config.StateIdKey) routeData.Add(key, data[key]); } VirtualPathData virtualPath = RouteTable.Routes.GetVirtualPath(new RequestContext(context, new RouteData()), GetRouteName(state, context), routeData); if (virtualPath == null) return null; return virtualPath.VirtualPath; }
/// <summary> /// Gets the data parsed from the Route and QueryString of the <paramref name="context"/> /// with the controller and action Route defaults removed /// </summary> /// <param name="state">The <see cref="State"/> navigated to</param> /// <param name="context">The current context</param> /// <returns>The navigation data</returns> public override NameValueCollection GetNavigationData(State state, HttpContextBase context) { NameValueCollection data = base.GetNavigationData(state, context); data.Remove("controller"); data.Remove("action"); if (context.Request.QueryString["refreshajax"] != null) { data.Remove("refreshajax"); data.Remove("includecurrent"); data.Remove("currentkeys"); data.Remove("tokeys"); data.Remove("navigation"); } return data; }
internal static void AddHttpRoute(State state) { string apiController = state.Attributes["apiController"] != null ? state.Attributes["apiController"].Trim() : string.Empty; string action = state.Attributes["action"] != null ? state.Attributes["action"].Trim() : string.Empty; if (apiController.Length != 0 && action.Length != 0) { if (!_Initialised) { Initialise(); _Initialised = true; } state.StateHandler = new WebApiStateHandler(); RouteValueDictionary defaults = StateInfoConfig.GetRouteDefaults(state, state.Route); defaults["controller"] = apiController; defaults["action"] = action; GlobalConfiguration.Configuration.Routes.MapHttpRoute("WebApi" + state.Id, state.Route, defaults); Route route = (Route)RouteTable.Routes["WebApi" + state.Id]; route.DataTokens = new RouteValueDictionary() { { NavigationSettings.Config.StateIdKey, state.Id } }; route.RouteHandler = new WebApiStateRouteHandler(state); } }
/// <summary> /// Gets the data parsed from the Route and QueryString of the <paramref name="context"/> /// </summary> /// <param name="state">The <see cref="Navigation.State"/> navigated to</param> /// <param name="context">The current context</param> /// <returns>The navigation data</returns> public virtual NameValueCollection GetNavigationData(State state, HttpContextBase context) { NameValueCollection queryData = new NameValueCollection(); RouteData routeData = context.Request.RequestContext.RouteData; if (routeData.DataTokens[NavigationSettings.Config.StateIdKey] != null) { queryData[NavigationSettings.Config.StateIdKey] = (string)routeData.DataTokens[NavigationSettings.Config.StateIdKey]; } foreach (string key in routeData.Values.Keys) { if (routeData.Values[key] != null) { queryData.Add(key, (string)routeData.Values[key]); } } foreach (string key in context.Request.QueryString) { queryData.Add(key, context.Request.QueryString[key]); } return queryData; }
internal static void AddRoutes(State state) { if (state.Page.Length != 0 && state.Attributes["route"] != null && state.Attributes["route"].Length != 0) { Route route = RouteTable.Routes.MapPageRoute(state.GetRouteName(false), state.GetRoute(false), state.GetPage(false), state.CheckPhysicalUrlAccess, StateInfoConfig.GetRouteDefaults(state, state.GetRoute(false)), null, new RouteValueDictionary() { { NavigationSettings.Config.StateIdKey, state.Id }, }); #if NET45Plus if (state.MobilePage.Length == 0 && state.MobileRoute.Length == 0 && state.MobileMasters.Count == 0 && state.MobileTheme.Length == 0) route.RouteHandler = (StateRouteHandler)Activator.CreateInstance(_StateRouteHandlerType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { state }, null); #endif } if (state.Page.Length != 0 && state.Attributes["mobileRoute"] != null && state.Attributes["mobileRoute"].Length != 0) RouteTable.Routes.MapPageRoute(state.GetRouteName(true), state.GetRoute(true), state.GetPage(true), state.CheckPhysicalUrlAccess, StateInfoConfig.GetRouteDefaults(state, state.GetRoute(true)), null, new RouteValueDictionary() { { NavigationSettings.Config.StateIdKey, state.Id }, }); }
/// <summary> /// Creates <see cref="Navigation.NavigationData"/> that corresponds to the key/value pairs /// specified by the <paramref name="expression"/></summary> /// <param name="expression">The key/value pairs with types optional. Values are optional if /// <paramref name="useCurrentData"/> is true</param> /// <param name="state">Holds the <see cref="Navigation.State.DefaultTypes"/> of the keys</param> /// <param name="useCurrentData">Indicates whether values can be retrieved from the current /// <see cref="Navigation.StateContext.Data"/></param> /// <returns>The <see cref="Navigation.NavigationData"/> that corresponds to the specified /// key/value pairs</returns> /// <exception cref="System.ArgumentNullException"><paramref name="expression"/> is null</exception> /// <exception cref="System.FormatException">Either the <paramref name="expression"/> was not /// in a recognised format or it contained an unrecognised type or a value was not in a format /// recognised by its corresponding type</exception> /// <exception cref="System.InvalidCastException">The <paramref name="expression"/> specifies types /// of guid or timespan</exception> /// <exception cref="System.OverflowException">A value represents a number that is out of the /// range of its corresponding type</exception> public static NavigationData ParseNavigationDataExpression(string expression, State state, bool useCurrentData) { if (expression == null) throw new ArgumentNullException("expression"); string[] keyTypeValue; expression = expression.Trim(); bool includeCurrentData = useCurrentData && expression.StartsWith("&", StringComparison.Ordinal); NavigationData navigationData = new NavigationData(includeCurrentData); if (includeCurrentData) { expression = expression.Substring(1); if (expression.Length == 0) return navigationData; } foreach (string dataItem in expression.Split(new char[] { ',' })) { keyTypeValue = dataItem.Split(new char[] { '=' }); if (keyTypeValue.Length == 2) SetNavigationKeyValue(navigationData, keyTypeValue, state); else SetNavigationKey(navigationData, keyTypeValue, useCurrentData, includeCurrentData); } return navigationData; }
/// <summary> /// Returns <paramref name="state"/> defaults for the specified <paramref name="route"/> parameters /// </summary> /// <param name="state">The <see cref="State"/> to check for default values</param> /// <param name="route">The route parameters</param> /// <returns><see cref="State"/> defaults for the supplied route</returns> public static RouteValueDictionary GetRouteDefaults(State state, string route) { RouteValueDictionary defaults = new RouteValueDictionary(); foreach (string key in state.FormattedDefaults.Keys) { if (route.IndexOf(string.Format(CultureInfo.InvariantCulture, PARAMETER, key), StringComparison.Ordinal) >= 0 || route.IndexOf(string.Format(CultureInfo.InvariantCulture, OPTIONAL_PARAMETER, key), StringComparison.Ordinal) >= 0) defaults.Add(key, state.FormattedDefaults[key]); } return defaults; }
internal MockHttpServerUtility(State state) : base() { State = state; }
private static StateDisplayInfo GetStateDisplayInfo(State state, HttpRequest request) { bool mobile; string displayModes = HttpUtility.HtmlDecode(request.Form[DISPLAY_MODES]); StateDisplayInfo stateDisplayInfo = new StateDisplayInfo(); if (displayModes == null) { mobile = request.Browser.IsMobileDevice; stateDisplayInfo.DisplayModes = mobile ? "Mobile" : string.Empty; stateDisplayInfo.IsPostBack = false; } else { mobile = StringComparer.OrdinalIgnoreCase.Compare(Regex.Split(displayModes, "\\|")[0], "Mobile") == 0; stateDisplayInfo.DisplayModes = displayModes; stateDisplayInfo.IsPostBack = true; } stateDisplayInfo.Page = state.GetPage(mobile); #if NET40Plus stateDisplayInfo.Route = state.GetRoute(mobile); stateDisplayInfo.RouteName = state.GetRouteName(mobile); #endif stateDisplayInfo.Masters = state.GetMasters(mobile); stateDisplayInfo.Theme = state.GetTheme(mobile); return stateDisplayInfo; }
/// <summary> /// Redirects or Transfers to the <paramref name="url"/> /// </summary> /// <param name="state">The <see cref="State"/> to navigate to</param> /// <param name="url">The target location</param> /// <exception cref="System.ArgumentNullException"><paramref name="state"/> is null</exception> public static void NavigateLink(State state, string url) { NavigateLink(state, url, NavigationMode.Client); }
internal StateDisplayInfo SetPageStateDisplay(Page page, State state) { HttpContextBase context = page.Request.RequestContext.HttpContext; StateDisplayInfo stateDisplayInfo = (StateDisplayInfo)context.Items[_StateDisplayInfoKey]; if (stateDisplayInfo == null || page.RouteData.Route == null) stateDisplayInfo = GetStateDisplayInfo(state, context); if (stateDisplayInfo.Masters.Count == 0 && !string.IsNullOrEmpty(page.MasterPageFile)) { List<string> masters = new List<string>(); masters.Add(page.MasterPageFile); stateDisplayInfo.Masters = new ReadOnlyCollection<string>(masters); } if (stateDisplayInfo.Theme.Length == 0 && !string.IsNullOrEmpty(page.Theme)) stateDisplayInfo.Theme = page.Theme; if (!stateDisplayInfo.IsPostBack) UpdateStateDisplayInfo(stateDisplayInfo, context, page); else UpdateStateDisplayInfo(stateDisplayInfo, page, context); return stateDisplayInfo; }
/// <summary> /// Redirects or Transfers to the <paramref name="url"/> depending on the /// <paramref name="mode"/> specified /// </summary> /// <param name="state">The <see cref="State"/> to navigate to</param> /// <param name="url">The target location</param> /// <param name="mode">Indicates whether to Redirect or Transfer</param> /// <exception cref="System.ArgumentNullException"><paramref name="state"/> is null</exception> public static void NavigateLink(State state, string url, NavigationMode mode) { if (state == null) throw new ArgumentNullException("state"); #if NET40Plus HttpContextBase context = null; if (HttpContext.Current != null && mode != NavigationMode.Mock) context = new HttpContextWrapper(HttpContext.Current); else context = new MockNavigationContext(url, state); state.StateHandler.NavigateLink(state, url, mode, context); #else state.StateHandler.NavigateLink(state, url, mode); #endif }
/// <summary> /// Overridden by derived classes to return an unprotected set of query string parameters /// </summary> /// <param name="data">A protected set of key/value pairs produced by the Encode method</param> /// <param name="state">The <see cref="Navigation.State"/> the Url has navigated to</param> /// <returns>Unprotected set of query string parameters</returns> public virtual NameValueCollection Decode(NameValueCollection data, State state) { return Decode(data); }
private static void ProcessStates(Dialog dialog, XmlNode dialogNode) { State state; int stateIndex = 0; XmlNode dialogChildNode; string[] derived; int i; bool result; #if NET40Plus string resourceType, resourceKey; #endif for (i = 0; i < dialogNode.ChildNodes.Count; i++) { state = new State(); dialogChildNode = dialogNode.ChildNodes[i]; if (dialogChildNode.NodeType != XmlNodeType.Comment) { if (dialogChildNode.Name == "state") { state.Parent = dialog; state.Index = stateIndex; stateIndex++; if (dialogChildNode.Attributes["key"] == null || dialogChildNode.Attributes["key"].Value.Length == 0) throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeMissing, "key")); state.Key = dialogChildNode.Attributes["key"].Value; state.Title = dialogChildNode.Attributes["title"] != null ? dialogChildNode.Attributes["title"].Value : string.Empty; #if NET40Plus resourceType = dialogChildNode.Attributes["resourceType"] != null ? dialogChildNode.Attributes["resourceType"].Value : "StateInfo"; resourceKey = dialogChildNode.Attributes["resourceKey"] != null ? dialogChildNode.Attributes["resourceKey"].Value : string.Empty; if (resourceKey.Length != 0) state.TitleFunc = () => (string)HttpContext.GetGlobalResourceObject(resourceType, resourceKey, Thread.CurrentThread.CurrentUICulture); #else state.ResourceType = dialogChildNode.Attributes["resourceType"] != null ? dialogChildNode.Attributes["resourceType"].Value : "StateInfo"; state.ResourceKey = dialogChildNode.Attributes["resourceKey"] != null ? dialogChildNode.Attributes["resourceKey"].Value : string.Empty; #endif #if NET40Plus state.Route = dialogChildNode.Attributes["route"] != null ? dialogChildNode.Attributes["route"].Value : string.Empty; #endif state.DefaultTypes = new StateInfoCollection<Type>(); if (dialogChildNode.Attributes["defaultTypes"] != null) { if (!TryParseDefaultTypes(dialogChildNode.Attributes["defaultTypes"].Value, state)) { throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeInvalid, state.Key, "defaultTypes")); } } state.Defaults = new StateInfoCollection<object>(); state.FormattedDefaults = new StateInfoCollection<string>(); if (dialogChildNode.Attributes["defaults"] != null) { try { NavigationData navigationData = StateInfoConfig.ParseNavigationDataExpression(dialogChildNode.Attributes["defaults"].Value, state, false); foreach (NavigationDataItem item in navigationData) { state.Defaults[item.Key] = item.Value; state.FormattedDefaults[item.Key] = CrumbTrailManager.FormatURLObject(item.Key, item.Value, state); } } catch (InvalidCastException ice) { throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeInvalid, state.Key, "defaults"), ice); } catch (FormatException fe) { throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeInvalid, state.Key, "defaults"), fe); } catch (OverflowException oe) { throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeInvalid, state.Key, "defaults"), oe); } } derived = new string[] { }; state.DerivedInternal = new Dictionary<string, string>(); if (dialogChildNode.Attributes["derived"] != null) { derived = Regex.Split(dialogChildNode.Attributes["derived"].Value, ","); for (int j = 0; j < derived.Length; j++) { derived[j] = derived[j].Trim(); state.DerivedInternal.Add(derived[j], derived[j]); } } state.Derived = new ReadOnlyCollection<string>(derived); state.TrackCrumbTrail = true; if (dialogChildNode.Attributes["trackCrumbTrail"] != null) { if (bool.TryParse(dialogChildNode.Attributes["trackCrumbTrail"].Value, out result)) state.TrackCrumbTrail = result; else throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.StateAttributeInvalid, state.Key, "trackCrumbTrail")); } if (dialog.States[dialogChildNode.Attributes["key"].Value] != null) throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicateStateKey, dialogChildNode.Attributes["key"].Value, dialog.Key)); state.Attributes = new StateInfoCollection<string>(); foreach (XmlAttribute attribute in dialogChildNode.Attributes) state.Attributes[attribute.Name] = attribute.Value; dialog.States[dialogChildNode.Attributes["key"].Value] = state; } } } }
private static void SetNavigationKeyValue(NavigationData navigationData, string[] keyTypeValue, State state) { string[] keyType = keyTypeValue[0].Trim().Split(new char[] { '?' }); if (keyType.Length > 2) throw new FormatException(Resources.InvalidNavigationDataExpression); string value = keyTypeValue[1].Trim(); string key = keyType[0].Trim(); if (string.IsNullOrEmpty(key)) throw new FormatException(Resources.InvalidNavigationDataExpression); Type type = typeof(string); if (state != null && state.DefaultTypes[key] != null) type = state.DefaultTypes[key]; if (keyType.Length == 2) type = StateInfoConfig.GetType(keyType[1].Trim().ToUpperInvariant()); if (type == null) throw new FormatException(Resources.InvalidNavigationDataExpression); object obj = Convert.ChangeType(value, type, CultureInfo.CurrentCulture); navigationData[key] = obj; }
private static bool TryParseDefaultTypes(string expression, State state) { string[] keyTypeValue; string key, type; foreach (string dataItem in expression.Split(new char[] { ',' })) { keyTypeValue = dataItem.Split(new char[] { '=' }); if (keyTypeValue.Length != 2) return false; type = keyTypeValue[1].Trim().ToUpperInvariant(); key = keyTypeValue[0].Trim(); if (StateInfoConfig.GetType(type) == null) return false; state.DefaultTypes[key] = StateInfoConfig.GetType(type); } return true; }
internal void SetCoords(State state, int value) { if (From == state && !X1.HasValue) X1 = value; else X2 = value; }
/// <summary> /// Overridden by derived classes to return an unprotected set of query string parameters /// </summary> /// <param name="data">A protected set of key/value pairs produced by the Encode method</param> /// <param name="historyPoint">Identifies if the Url is being decoded as a result of /// a call to <see cref="StateController.NavigateHistory"/></param> /// <param name="state">The <see cref="Navigation.State"/> the Url has navigated to</param> /// <returns>Unprotected set of query string parameters</returns> public virtual NameValueCollection Decode(NameValueCollection data, bool historyPoint, State state) { return Decode(data, historyPoint); }
private static StringBuilder FormatReturnData(StringBuilder returnDataBuilder, State state, NavigationData returnData) { returnDataBuilder = returnDataBuilder ?? new StringBuilder(); string prefix = string.Empty; foreach (NavigationDataItem item in returnData) { if (!item.Value.Equals(string.Empty) && !state.DefaultOrDerived(item.Key, item.Value)) { returnDataBuilder.Append(prefix); returnDataBuilder.Append(EncodeURLValue(item.Key)); returnDataBuilder.Append(RET_1_SEP); returnDataBuilder.Append(FormatURLObject(item.Key, item.Value, state)); prefix = RET_3_SEP; } } return returnDataBuilder; }
public StateElement(State state) { State = state; }
internal static string FormatURLObject(string key, object urlObject, State state) { Type defaultType = state.DefaultTypes[key] ?? typeof(string); string converterKey = ConverterFactory.GetKey(urlObject); string formattedValue = ConverterFactory.GetConverter(converterKey).ConvertToInvariantString(urlObject); formattedValue = EncodeURLValue(formattedValue); if (urlObject.GetType() != defaultType) formattedValue += RET_2_SEP + converterKey; return formattedValue; }
private static StateDisplayInfo GetStateDisplayInfo(State state, HttpContextBase context) { bool mobile; string displayModes = HttpUtility.HtmlDecode(context.Request.Form[DISPLAY_MODES]); StateDisplayInfo stateDisplayInfo = new StateDisplayInfo(); if (displayModes == null) { mobile = new HttpContextWrapper(HttpContext.Current).GetOverriddenBrowser().IsMobileDevice; if (DisplayModeProvider.Instance.RequireConsistentDisplayMode && (!mobile || state.MobilePage.Length == 0)) stateDisplayInfo.DisplayMode = DisplayModeProvider.Instance.Modes.Last(); stateDisplayInfo.DisplayModes = mobile ? "Mobile" : string.Empty; stateDisplayInfo.IsPostBack = false; } else { mobile = StringComparer.OrdinalIgnoreCase.Compare(Regex.Split(displayModes, "\\|")[0], "Mobile") == 0; stateDisplayInfo.DisplayModes = displayModes; stateDisplayInfo.IsPostBack = true; } stateDisplayInfo.Page = state.GetPage(mobile); stateDisplayInfo.Route = state.GetRoute(mobile); stateDisplayInfo.RouteName = state.GetRouteName(mobile); stateDisplayInfo.Masters = state.GetMasters(mobile); stateDisplayInfo.Theme = state.GetTheme(mobile); return stateDisplayInfo; }
internal static object ParseURLString(string key, string val, State state) { Type defaultType = state.DefaultTypes[key] ?? typeof(string); string urlValue = val; string converterKey = ConverterFactory.GetKey(defaultType); if (val.IndexOf(RET_2_SEP, StringComparison.Ordinal) > -1) { string[] arr = Regex.Split(val, RET_2_SEP); urlValue = arr[0]; converterKey = arr[1]; } try { return ConverterFactory.GetConverter(converterKey).ConvertFromInvariantString(DecodeURLValue(urlValue)); } catch (Exception ex) { throw new UrlException(Resources.InvalidUrl, ex); } }
internal StateRouteHandler(State state) { State = state; }
internal static object Parse(string key, string val, State state) { object parsedVal; if (key == NavigationSettings.Config.ReturnDataKey) { parsedVal = ParseReturnData(val, state); } else { if (key == NavigationSettings.Config.CrumbTrailKey) { parsedVal = val; } else { parsedVal = ParseURLString(key, val, state); } } return parsedVal; }
internal static StateDisplayInfo SetPageStateDisplay(Page page, State state) { StateDisplayInfo stateDisplayInfo = GetStateDisplayInfo(state, page.Request); if (stateDisplayInfo.Masters.Count == 0 && !string.IsNullOrEmpty(page.MasterPageFile)) { List<string> masters = new List<string>(); masters.Add(page.MasterPageFile); stateDisplayInfo.Masters = new ReadOnlyCollection<string>(masters); } if (stateDisplayInfo.Theme.Length == 0 && !string.IsNullOrEmpty(page.Theme)) stateDisplayInfo.Theme = page.Theme; if (!stateDisplayInfo.IsPostBack) UpdateStateDisplayInfo(stateDisplayInfo, page.Request, page); else UpdateStateDisplayInfo(stateDisplayInfo, page); return stateDisplayInfo; }
private static NavigationData ParseReturnData(string returnData, State state) { NavigationData navData = new NavigationData(); string[] nameValuePair; string[] returnDataArray = Regex.Split(returnData, RET_3_SEP); for (int i = 0; i < returnDataArray.Length; i++) { nameValuePair = Regex.Split(returnDataArray[i], RET_1_SEP); navData.Add(DecodeURLValue(nameValuePair[0]), ParseURLString(DecodeURLValue(nameValuePair[0]), nameValuePair[1], state)); } return navData; }