/// <summary> /// Appends the path info. /// </summary> /// <param name="parts">The parts.</param> /// <param name="parameters">The parameters.</param> protected virtual void AppendPathInfo(UrlParts parts, UrlBuilderParameters parameters) { if (!string.IsNullOrEmpty(parameters.PathInfo)) { parts.PathInfoDict.Parse(parameters.PathInfo); } }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public virtual UrlParts CreateUrlPartsBuilder(UrlInfo current, IDictionary parameters, IDictionary routeParameters) { AssertArguments(current, parameters); var typedParams = UrlBuilderParameters.From(parameters, routeParameters); return(CreateUrlPartsBuilder(current, parameters, ConvertRouteParams(typedParams.RouteParameters))); }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public virtual string BuildUrl(UrlInfo current, IDictionary parameters, IDictionary routeParameters) { AssertArguments(current, parameters); var typedParams = UrlBuilderParameters.From(parameters, routeParameters); return(BuildUrl(current, typedParams, ConvertRouteParams(typedParams.RouteParameters))); }
/// <summary> /// Applies the base path or absolute path if necessary. /// </summary> /// <param name="appVirtualDir">The app virtual dir.</param> /// <param name="area">The area.</param> /// <param name="current">The current.</param> /// <param name="parameters">The parameters.</param> /// <param name="path">The path.</param> /// <returns></returns> protected virtual string ApplyBasePathOrAbsolutePathIfNecessary(string appVirtualDir, string area, UrlInfo current, UrlBuilderParameters parameters, string path) { var createAbsolutePath = parameters.CreateAbsolutePath; var basePath = parameters.BasePath; if (!string.IsNullOrEmpty(basePath)) { basePath = basePath[basePath.Length - 1] == '/' ? basePath.Substring(0, basePath.Length - 1) : basePath; if (basePath.EndsWith(area, StringComparison.InvariantCultureIgnoreCase)) { path = basePath; } else { path = basePath + "/" + area; } } else if (createAbsolutePath) { var domain = parameters.Domain ?? current.Domain; var subdomain = parameters.Subdomain ?? current.Subdomain; var protocol = parameters.Protocol ?? current.Protocol; var port = parameters.Port == 0 ? current.Port : parameters.Port; var includePort = (protocol == "http" && port != 80) || (protocol == "https" && port != 443); path = protocol + "://"; if (!string.IsNullOrEmpty(subdomain)) { path += subdomain + "." + domain; } else { path += domain; } if (includePort) { path += ":" + port; } path += ComputeStandardBasePath(appVirtualDir, area); } return(path); }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public virtual string BuildUrl(UrlInfo current, UrlBuilderParameters parameters, IDictionary routeParameters) { AssertArguments(current, parameters); var encodeForLink = parameters.EncodeForLink; var url = CreateUrlPartsBuilder(current, parameters, routeParameters); if (encodeForLink) { return(url.BuildPathForLink()); } return(url.BuildPath()); }
/// <summary> /// Appends the query string. /// </summary> /// <param name="parts">The parts.</param> /// <param name="parameters">The parameters.</param> protected virtual void AppendQueryString(UrlParts parts, UrlBuilderParameters parameters) { var queryString = parameters.QueryString; var suffix = string.Empty; if (queryString != null) { if (queryString is IDictionary) { var qsDictionary = (IDictionary)queryString; suffix = CommonUtils.BuildQueryString(serverUtil, qsDictionary, false); } else if (queryString is NameValueCollection) { suffix = CommonUtils.BuildQueryString(serverUtil, (NameValueCollection)queryString, false); } else if (queryString is string && ((string)queryString).Length > 0) { var pairs = queryString.ToString().Split('&'); suffix = string.Empty; foreach (var pair in pairs) { var keyvalues = pair.Split(new[] { '=' }, 2); if (keyvalues.Length < 2) { continue; } if (suffix.Length != 0) { suffix += "&"; } suffix += serverUtil.UrlEncode(keyvalues[0]) + "=" + serverUtil.UrlEncode(keyvalues[1]); } } } if (suffix != string.Empty) { parts.SetQueryString(suffix); } }
/// <summary> /// Tries the create URL using registered routes. /// </summary> /// <param name="domain">The domain.</param> /// <param name="parameters">The parameters.</param> /// <param name="appVirtualDir">The app virtual dir.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> protected UrlParts TryCreateUrlUsingRegisteredRoutes(string domain, UrlBuilderParameters parameters, string appVirtualDir, IDictionary routeParameters) { if (routingEng != null && !routingEng.IsEmpty) { // We take was was explicitly set (we do not inherit those from the context) routeParameters["area"] = parameters.Area ?? routeParameters["area"]; routeParameters["controller"] = parameters.Controller ?? routeParameters["controller"]; routeParameters["action"] = parameters.Action ?? routeParameters["action"]; if (parameters.UseCurrentRouteParams) { if (parameters.RouteMatch == null) { throw new InvalidOperationException("Error creating URL. 'UseCurrentRouteParams' was set, but routematch is null."); } CommonUtils.MergeOptions(routeParameters, parameters.RouteMatch.Parameters); } string url; if (parameters.RouteName != null) { url = routingEng.CreateUrl(parameters.RouteName, routeParameters); } //If we want to UseCurrentRouteParams and the current RouteMatch has a name than we should create the url using the current route name else if (parameters.UseCurrentRouteParams && parameters.RouteMatch != null && !String.IsNullOrEmpty(parameters.RouteMatch.Name)) { url = routingEng.CreateUrl(parameters.RouteMatch.Name, routeParameters); } else { url = routingEng.CreateUrl(routeParameters); } if (url != null) { return(new UrlParts(serverUtil, url)); } } return(null); }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <param name="routeParameters">The route parameters.</param> /// <returns></returns> public virtual UrlParts CreateUrlPartsBuilder(UrlInfo current, UrlBuilderParameters parameters, IDictionary routeParameters) { AssertArguments(current, parameters); var appVirtualDir = current.AppVirtualDir; var area = parameters.Area ?? current.Area; var controller = parameters.Controller ?? current.Controller; var action = parameters.Action ?? current.Action; if (appVirtualDir.Length > 0 && !(appVirtualDir[0] == '/')) { appVirtualDir = "/" + appVirtualDir; } var path = ComputeStandardBasePath(appVirtualDir, area); path = ApplyBasePathOrAbsolutePathIfNecessary(appVirtualDir, area, current, parameters, path); var parts = TryCreateUrlUsingRegisteredRoutes(current.Domain, parameters, appVirtualDir, routeParameters); if (parts == null) { parts = new UrlParts(serverUtil, path, controller, action + (useExtensions ? SafeExt(current.Extension) : "")); AppendPathInfo(parts, parameters); } else { if (parameters.CreateAbsolutePath) { parts.InsertFrontPath(path); } else if (appVirtualDir != "/") { parts.InsertFrontPath(appVirtualDir); } } AppendQueryString(parts, parameters); return(parts); }
public void UseCurrentRouteParamsShouldBeHonoredRegardlessOfTheRoutingOrder() { var urlInfo = new UrlInfo("Services", "ModifyServiceWizard", "Step1", String.Empty, String.Empty); var parameters = new UrlBuilderParameters { RouteMatch = new RouteMatch { Name = "ServiceWizardModify" } }; parameters.RouteMatch.AddNamed("serviceArea", "Marketing"); parameters.RouteMatch.AddNamed("action", "Step1"); parameters.RouteMatch.AddNamed("controller", "ModifyServiceWizard"); parameters.RouteMatch.AddNamed("area", "Services"); parameters.UseCurrentRouteParams = true; IDictionary routeParameters = new HybridDictionary { { "action", "Step2" } }; IRoutingEngine routingEngine = new StubRoutingEngine(); routingEngine.Add( new PatternRoute("ServiceWizardCreate", "/Services/<serviceArea>/AddWizard/[action]") .DefaultForController().Is("CreateServiceWizard") .DefaultForArea().Is("Services") .DefaultForAction().Is("start") ); routingEngine.Add( new PatternRoute("ServiceWizardModify", "/Services/<serviceArea>/ModifyWizard/[action]") .DefaultForController().Is("ModifyServiceWizard") .DefaultForArea().Is("Services") .DefaultForAction().Is("start") ); urlBuilder.RoutingEngine = routingEngine; Assert.AreEqual("/Services/Marketing/ModifyWizard/Step2", urlBuilder.BuildUrl(urlInfo, parameters, routeParameters)); }
public void RouteParametersShouldBePersistedDuringCreateUrlPartsWhenNoneSpecifiedInParameters() { var urlInfo = new UrlInfo("i", "shouldbe", "overridden", "/", ".castle"); var parameters = new UrlBuilderParameters();//empty collection IDictionary routeParameters = new HybridDictionary { { "area", "routearea" }, { "controller", "routecontroller" }, { "action", "routeaction" } }; IRoutingEngine routingEngine = new StubRoutingEngine(); routingEngine.Add(new PatternRoute("default", "<area>/<controller>/<action>"));//keep routing engine from being empty urlBuilder.RoutingEngine = routingEngine; Assert.AreEqual("/routearea/routecontroller/routeaction", urlBuilder.BuildUrl(urlInfo, parameters, routeParameters)); }
public void UseAbsPathWithWWW() { var tokenizer = new DefaultUrlTokenizer(); var urlinfo = tokenizer.TokenizeUrl("/area/home/index.castle", null, new Uri("http://www.castleproject.org"), true, string.Empty); var parameters = new UrlBuilderParameters("test", "action") { CreateAbsolutePath = true }; Assert.AreEqual("http://www.castleproject.org/area/test/action.castle", urlBuilder.BuildUrl(urlinfo, parameters)); }
private static NavItem Item(IEngineContext context, string caption, string controller, string action) { var parameters = new UrlBuilderParameters(controller, action); return new NavItem { Caption = caption, Url = context.Services.UrlBuilder.BuildUrl(context.UrlInfo, parameters) }; }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> public virtual UrlParts CreateUrlPartsBuilder(UrlInfo current, UrlBuilderParameters parameters) { AssertArguments(current, parameters); return(CreateUrlPartsBuilder(current, parameters, ConvertRouteParams(parameters.RouteParameters))); }
/// <summary> /// Builds the URL using the current url as contextual information and the specified parameters. /// <para> /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/> /// for more information regarding the parameters. /// </para> /// </summary> /// <param name="current">The current Url information.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> public virtual string BuildUrl(UrlInfo current, UrlBuilderParameters parameters) { AssertArguments(current, parameters); return(BuildUrl(current, parameters, ConvertRouteParams(parameters.RouteParameters))); }
public void If_Route_Name_Is_Specified_It_Should_Be_Used_Even_If_UseCurrentRouteParams_Is_True() { var urlInfo = new UrlInfo("", "Car", "View", String.Empty, String.Empty); var parameters = new UrlBuilderParameters { RouteMatch = new RouteMatch { Name = "CarRoute" } }; parameters.RouteMatch.AddNamed("carName", "Ford"); parameters.RouteMatch.AddNamed("action", "View"); parameters.RouteMatch.AddNamed("controller", "Car"); parameters.UseCurrentRouteParams = true; parameters.RouteName = "CarAddOptionWizard"; IRoutingEngine routingEngine = new StubRoutingEngine(); routingEngine.Add( new PatternRoute("CarRoute", "/Car/<carName>/[action]") .DefaultForController().Is("Car") .DefaultForAction().Is("VIew") ); routingEngine.Add( new PatternRoute("CarAddOptionWizard", "/Car/<carName>/AddOption/[action]") .DefaultForController().Is("CarAddOptionWizard") .DefaultForAction().Is("start") ); urlBuilder.RoutingEngine = routingEngine; Assert.AreEqual("/Car/Ford/AddOption", urlBuilder.BuildUrl(urlInfo, parameters)); }
public void ShouldWorkForSingleLetterAppVirtualDir() { var tokenizer = new DefaultUrlTokenizer(); var urlinfo = tokenizer.TokenizeUrl("/v/area/controller/action.castle", null, new Uri("http://www.castleproject.org/v/area/controller/action.castle"), true, "/v"); var parameters = new UrlBuilderParameters(); Assert.AreEqual("/v/area/controller/action.castle", urlBuilder.BuildUrl(urlinfo, parameters)); }
protected virtual void InitializePropertyBagForCulture( CultureInfo culture ) { UrlBuilderParameters parameters = new UrlBuilderParameters( "Culture", "SetCulture" ); parameters.QueryString = DictHelper.CreateN( "cultureCode", culture.Name ).N( "backUrl", EngineContext.Request.Url ); bool isCurrent = CultureInfo.CurrentCulture.Name == culture.Name || ( !CultureInfo.CurrentCulture.IsNeutralCulture && CultureInfo.CurrentCulture.Parent.Name.Equals( culture.Name ) ); PropertyBag[ "CultureCulture" ] = culture; PropertyBag[ "IsCurrent" ] = isCurrent; PropertyBag[ "SelectionUrl" ] = EngineContext.Services.UrlBuilder.BuildUrl( EngineContext.UrlInfo, parameters ); PropertyBag[ "FlagImageUrl" ] = _ResourceHelper.GetImageResourceUrl( GetFlagResourceName( culture ) ); }