/// <summary> /// Process the defaults of the specified route /// </summary> /// <param name="route">Route to process</param> /// <param name="routeInfo">Output information about the route</param> public void ProcessDefaults(Route route, RouteInfo routeInfo) { routeInfo.Defaults = new RouteValueDictionary(); routeInfo.Optional = new List<string>(); if (route.Defaults == null) return; foreach (var kvp in route.Defaults) { if (kvp.Value == UrlParameter.Optional) { routeInfo.Optional.Add(kvp.Key); } else if (ShouldAddDefault(kvp.Key)) { routeInfo.Defaults.Add(kvp.Key, kvp.Value); } } // Add area if it's specified in the route if (route.DataTokens != null && route.DataTokens.ContainsKey(AREA_TOKEN)) { routeInfo.Defaults.Add("area", route.DataTokens[AREA_TOKEN]); } }
/// <summary>Initializes the thread culture based on the current requested language</summary> /// <param name="requestContext"></param> /// <created author="laurentiu.macovei" date="Sat, 12 Nov 2011 00:13:18 GMT"/> protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { var language = requestContext.RouteData.Values["language"] as string; if (!string.IsNullOrWhiteSpace(language) && (!LocalizationAppConfig.SupportedLanguages.Any() || LocalizationAppConfig.SupportedLanguages.Contains(language, StringComparer.OrdinalIgnoreCase))) try { var request =requestContext.HttpContext.Request; if (request.Url.PathAndQuery.StartsWith(FormsAuthentication.LoginUrl, StringComparison.OrdinalIgnoreCase)) { var returnUrl = request.QueryString["returnUrl"]; if (!string.IsNullOrWhiteSpace(returnUrl)) { var routeInfo = new RouteInfo(new Uri(request.Url, returnUrl), HttpContext.Current.Request.ApplicationPath); var returnUrlLanguage = routeInfo.RouteData.Values["language"] as string; language = string.IsNullOrWhiteSpace(returnUrlLanguage) ? language : returnUrlLanguage; } } if (language == "lib" || language == "css") requestContext.RouteData.Values["language"] = language = "en"; var cultureInfo = CultureInfo.CreateSpecificCulture(language); if (cultureInfo != null) { Thread.CurrentThread.CurrentUICulture = cultureInfo; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name); } } catch { //if invalid culture, do nothing } return base.GetHttpHandler(requestContext); }
public void HandlesRouteWithoutDefaults() { var route = new Route("hello/world", null, null); var routeInfo = new RouteInfo(); var defaultsProcessor = new DefaultsProcessor(); defaultsProcessor.ProcessDefaults(route, routeInfo); Assert.AreEqual(0, routeInfo.Defaults.Count); Assert.AreEqual(0, routeInfo.Optional.Count); }
/// <summary> /// Renders a child action into a dynamically created special tab called 'Plugins' /// </summary> /// <param name="routeInfo"></param> /// <remarks>Should only be called for admin tabstrips</remarks> public void AddWidget(RouteInfo routeInfo) { Guard.ArgumentNotNull(() => routeInfo); if (_widgets == null) { _widgets = new List<RouteInfo>(); Html.ViewContext.ViewData["Tab.{0}.Widgets".FormatInvariant(this.TabStripName)] = _widgets; CreateWidgetsTab(); } _widgets.Add(routeInfo); }
public void HandlesOptionalParameters() { var routes = new RouteCollection(); routes.MapRoute( name: "Hello", url: "hello/world/{id}", defaults: new { controller = "Hello", action = "HelloWorld", id = UrlParameter.Optional } ); var routeInfo = new RouteInfo(); var defaultsProcessor = new DefaultsProcessor(); defaultsProcessor.ProcessDefaults((Route)routes["Hello"], routeInfo); Assert.IsTrue(routeInfo.Optional.Contains("id")); }
public void HandlesAreas() { var routes = new RouteCollection(); var areaReg = new AreaRegistrationContext("Foobar", routes); areaReg.MapRoute( name: "Hello", url: "hello/world", defaults: new { controller = "Hello", action = "HelloWorld" } ); var routeInfo = new RouteInfo(); var defaultsProcessor = new DefaultsProcessor(); defaultsProcessor.ProcessDefaults((Route)routes["Hello"], routeInfo); Assert.AreEqual("Foobar", routeInfo.Defaults["area"]); }
public void HandlesMvcControllerAndAction() { var routes = new RouteCollection(); routes.MapRoute( name: "Hello", url: "hello/world", defaults: new { controller = "Hello", action = "HelloWorld" } ); var routeInfo = new RouteInfo(); var defaultsProcessor = new DefaultsProcessor(); defaultsProcessor.ProcessDefaults((Route)routes["Hello"], routeInfo); Assert.AreEqual("Hello", routeInfo.Defaults["controller"]); Assert.AreEqual("HelloWorld", routeInfo.Defaults["action"]); }
public void HandlesTwoOptionalParameters() { var routes = new RouteCollection(); routes.MapRoute( name: "Hello", url: "hello/world/{first}/{second}", defaults: new { controller = "Hello", action = "Index", first = UrlParameter.Optional, second = UrlParameter.Optional } ); var processor = new DefaultsProcessor(); var routeInfo = new RouteInfo(); processor.ProcessDefaults((Route)routes["Hello"], routeInfo); Assert.AreEqual(2, routeInfo.Defaults.Count); Assert.AreEqual(2, routeInfo.Optional.Count); Assert.AreEqual("first", routeInfo.Optional[0]); Assert.AreEqual("second", routeInfo.Optional[1]); }
public void HandlesOneOptionalParameter() { var routes = new RouteCollection(); routes.MapRoute( name: "Hello", url: "hello/world/{id}", defaults: new { controller = "Hello", action = "Index", id = UrlParameter.Optional } ); var processor = new DefaultsProcessor(); var routeInfo = new RouteInfo(); processor.ProcessDefaults((Route)routes["Hello"], routeInfo); Assert.AreEqual(2, routeInfo.Defaults.Count); Assert.AreEqual("Hello", routeInfo.Defaults["controller"]); Assert.AreEqual("Index", routeInfo.Defaults["action"]); Assert.AreEqual(1, routeInfo.Optional.Count); Assert.AreEqual("id", routeInfo.Optional[0]); }
/// <summary>Initializes the thread culture based on the current requested language</summary> /// <param name="requestContext"></param> /// <created author="laurentiu.macovei" date="Sat, 12 Nov 2011 00:13:18 GMT"/> protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { var language = requestContext.RouteData.Values["language"] as string; if (!string.IsNullOrWhiteSpace(language) && (!LocalizationAppConfig.SupportedLanguages.Any() || LocalizationAppConfig.SupportedLanguages.Contains(language, StringComparer.OrdinalIgnoreCase))) { try { var request = requestContext.HttpContext.Request; if (request.Url.PathAndQuery.StartsWith(FormsAuthentication.LoginUrl, StringComparison.OrdinalIgnoreCase)) { var returnUrl = request.QueryString["returnUrl"]; if (!string.IsNullOrWhiteSpace(returnUrl)) { var routeInfo = new RouteInfo(new Uri(request.Url, returnUrl), HttpContext.Current.Request.ApplicationPath); var returnUrlLanguage = routeInfo.RouteData.Values["language"] as string; language = string.IsNullOrWhiteSpace(returnUrlLanguage) ? language : returnUrlLanguage; } } if (language == "lib" || language == "css") { requestContext.RouteData.Values["language"] = language = "en"; } var cultureInfo = CultureInfo.CreateSpecificCulture(language); if (cultureInfo != null) { Thread.CurrentThread.CurrentUICulture = cultureInfo; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name); } } catch { //if invalid culture, do nothing } } return(base.GetHttpHandler(requestContext)); }
/// <summary> /// </summary> /// <param name="uri"></param> /// <param name="controllerName"></param> /// <param name="actionName"></param> /// <returns></returns> /// <created author="scott.schluer" date="Fri, 18 Nov 2011 14:28:22 GMT"/> public static bool IsRouteMatch(this Uri uri, string controllerName, string actionName) { RouteInfo routeInfo = new RouteInfo(uri, HttpContext.Current.Request.ApplicationPath); return (routeInfo.RouteData.Values["controller"].ToString() == controllerName && routeInfo.RouteData.Values["action"].ToString() == actionName); }
/// <summary> /// </summary> /// <param name="uri"></param> /// <param name="paramaterName"></param> /// <returns></returns> /// <created author="scott.schluer" date="Fri, 18 Nov 2011 14:28:22 GMT"/> public static string GetRouteParameterValue(this Uri uri, string paramaterName) { RouteInfo routeInfo = new RouteInfo(uri, HttpContext.Current.Request.ApplicationPath); return routeInfo.RouteData.Values[paramaterName] != null ? routeInfo.RouteData.Values[paramaterName].ToString() : null; }