Beispiel #1
0
        private static UrlParts CreateForRelativePath(IServerUtility serverUtility, string url)
        {
            var    path     = url;
            string qs       = null;
            string pathInfo = null;

            var queryStringStartIndex = url.IndexOf('?');

            if (queryStringStartIndex != -1)
            {
                qs   = url.Substring(queryStringStartIndex);
                path = url.Substring(0, queryStringStartIndex);
            }

            var fileExtIndex = path.IndexOf('.');

            if (fileExtIndex != -1)
            {
                var pathInfoStartIndex = path.IndexOf('/', fileExtIndex);

                if (pathInfoStartIndex != -1)
                {
                    pathInfo = path.Substring(pathInfoStartIndex);
                    path     = path.Substring(0, pathInfoStartIndex);
                }
            }

            var parts = new UrlParts(serverUtility, path);

            parts.SetQueryString(qs);
            parts.PathInfoDict.Parse(pathInfo);

            return(parts);
        }
		public void PathInfoDictParse_AcceptsNull()
		{
			var builder = new UrlParts(serverUtil, "controller", "action");
			builder.PathInfoDict.Parse(null);

			Assert.AreEqual("controller/action", builder.BuildPath());
		}
		public void PathInfoDictParse_AcceptsEmptyString()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfoDict.Parse("");

			Assert.AreEqual("controller/action", builder.BuildPath());
		}
		public void PathInfoDictParse_CanHandleMissingSlash()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfoDict.Parse("State/Fl");

			Assert.AreEqual("controller/action/State/Fl", builder.BuildPath());
		}
		public void BuildPathWithPathInfoDictionary()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfoDict["State"] ="FL";

			Assert.AreEqual("controller/action/State/FL", builder.BuildPath());
		}
		public void BuildPathWithPathInfo()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfo.Add("State").Add("FL");

			Assert.AreEqual("controller/action/State/FL", builder.BuildPath());
		}
Beispiel #7
0
 /// <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);
     }
 }
		public void BuildPathWithPathInfoAndQueryString()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfoDict["State"] = "FL";
			builder.SetQueryString("type=Residential");

			Assert.AreEqual("controller/action/State/FL?type=Residential", builder.BuildPath());
		}
Beispiel #9
0
        /// <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);
            }
        }
Beispiel #10
0
        private static UrlParts CreateForAbsolutePath(IServerUtility serverUtility, Uri uri)
        {
            var host = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.PathAndQuery.Length);

            var parts = new UrlParts(serverUtility, host);

            foreach (var segment in uri.Segments)
            {
                parts.AppendPath(segment);
            }

            parts.ConvertPathInfoToDict();
            parts.SetQueryString(uri.Query);

            return(parts);
        }
Beispiel #11
0
        /// <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 InsertFrontPath_ShouldHandleTwoPathsWithSlash()
		{
			UrlParts builder = new UrlParts();
			builder.AppendPath("/something");

			builder.InsertFrontPath("http://something/else/");

			Assert.AreEqual("http://something/else/something", builder.BuildPath());
		}
		public void InsertFrontPath_ShouldKeepExistingPath()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfo.Add("State").Add("FL");

			builder.InsertFrontPath("http://something");

			Assert.AreEqual("http://something/controller/action/State/FL", builder.BuildPath());
		}
		public void QueryStringCanReplaceEntries()
		{
			UrlParts builder = new UrlParts("home/index.ext");

			builder.QueryString["page"] = "1";

			Assert.AreEqual("home/index.ext?page=1", builder.BuildPath());

			builder.QueryString.Set("page", "2");

			Assert.AreEqual("home/index.ext?page=2", builder.BuildPath());

			builder.QueryString.Set("page", "3");

			Assert.AreEqual("home/index.ext?page=3", builder.BuildPath());
		}
		public void QueryStringCanHandleDuplicatedEntries()
		{
			UrlParts builder = new UrlParts("home/index.ext");

			builder.SetQueryString("City=SP&State=MD&State=NY");

			Assert.AreEqual("home/index.ext?City=SP&State=MD&State=NY", builder.BuildPath());
		}
		public void QueryStringIsExtractedAndParsed()
		{
			UrlParts builder = new UrlParts("home/index.ext");

			builder.SetQueryString("City=SP&State=MD");

			builder.QueryString["type"] = "home";

			Assert.AreEqual("home/index.ext?City=SP&State=MD&type=home", builder.BuildPath());
		}
		public void CanBuildPathUrls()
		{
			UrlParts builder = new UrlParts("controller", "action");
		
			Assert.AreEqual("controller/action", builder.BuildPath());
		}
		public void QueryStringParsesStringCorrectly()
		{
			UrlParts builder = new UrlParts("home/index.ext");

			builder.QueryString["state"] = "FL";

			Assert.AreEqual("home/index.ext?state=FL", builder.BuildPath());
		}
Beispiel #19
0
		private static UrlParts CreateForRelativePath(string url)
		{
			string path = url;
			string qs = null;
			string pathInfo = null;

			int queryStringStartIndex = url.IndexOf('?');
			int fileExtIndex = url.IndexOf('.');

			if (queryStringStartIndex != -1)
			{
				qs = url.Substring(queryStringStartIndex);
				path = url.Substring(0, queryStringStartIndex);
			}

			if (fileExtIndex != -1)
			{
				int pathInfoStartIndex = path.IndexOf('/', fileExtIndex);

				if (pathInfoStartIndex != -1)
				{
					pathInfo = path.Substring(pathInfoStartIndex);
					path = path.Substring(0, pathInfoStartIndex);
				}
			}

			UrlParts parts = new UrlParts(path);
			parts.SetQueryString(qs);
			parts.PathInfoDict.Parse(pathInfo);

			return parts;
		}
Beispiel #20
0
			/// <summary>
			/// Initializes a new instance of the <see cref="PathInfoBuilder"/> class.
			/// </summary>
			/// <param name="parent">The parent.</param>
			public PathInfoDictBuilder(UrlParts parent)
			{
				this.parent = parent;
				parameters = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
			}
Beispiel #21
0
			/// <summary>
			/// Initializes a new instance of the <see cref="PathInfoBuilder"/> class.
			/// </summary>
			/// <param name="parent">The parent.</param>
			public PathInfoBuilder(UrlParts parent)
			{
				this.parent = parent;
				pieces = new List<string>();
			}
Beispiel #22
0
		private static UrlParts CreateForAbsolutePath(Uri uri)
		{
			string host = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.PathAndQuery.Length);

			UrlParts parts = new UrlParts(host);

			foreach (string segment in uri.Segments)
			{
				parts.AppendPath(segment);
			}

			parts.ConvertPathInfoToDict();
			parts.SetQueryString(uri.Query);

			return parts;
		}
Beispiel #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PathInfoBuilder"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public PathInfoBuilder(UrlParts parent)
 {
     this.parent = parent;
     pieces      = new List <string>();
 }
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PathInfoBuilder"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public PathInfoDictBuilder(UrlParts parent)
 {
     this.parent = parent;
     parameters  = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
 }
		public void PathInfoDictParse_CanHandleMultipleEntries()
		{
			var builder = new UrlParts(serverUtil, "controller", "action");
			builder.PathInfoDict.Parse("/State/FL/Type/Home");

			Assert.AreEqual("controller/action/State/FL/Type/Home", builder.BuildPath());
		}
		private static UrlParts CreateForAbsolutePath(IServerUtility serverUtility, Uri uri)
		{
			var host = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.PathAndQuery.Length);

			var parts = new UrlParts(serverUtility, host);

			foreach (var segment in uri.Segments)
			{
				parts.AppendPath(segment);
			}

			parts.ConvertPathInfoToDict();
			parts.SetQueryString(uri.Query);

			return parts;
		}
		public void PathInfoDictParse_CanHandleOddNumberOfEntries()
		{
			UrlParts builder = new UrlParts("controller", "action");
			builder.PathInfoDict.Parse("/State/FL/Type/");

			Assert.AreEqual("controller/action/State/FL/Type", builder.BuildPath());
		}
		private static UrlParts CreateForRelativePath(IServerUtility serverUtility, string url)
		{
			var path = url;
			string qs = null;
			string pathInfo = null;

			var queryStringStartIndex = url.IndexOf('?');

			if (queryStringStartIndex != -1)
			{
				qs = url.Substring(queryStringStartIndex);
				path = url.Substring(0, queryStringStartIndex);
			}

			var fileExtIndex = path.IndexOf('.');

			if (fileExtIndex != -1)
			{
				var pathInfoStartIndex = path.IndexOf('/', fileExtIndex);

				if (pathInfoStartIndex != -1)
				{
					pathInfo = path.Substring(pathInfoStartIndex);
					path = path.Substring(0, pathInfoStartIndex);
				}
			}

			var parts = new UrlParts(serverUtility, path);
			parts.SetQueryString(qs);
			parts.PathInfoDict.Parse(pathInfo);

			return parts;
		}