/// <summary> /// Izdvaja querz segment /// </summary> /// <param name="query"></param> /// <param name="targetSegment"></param> /// <param name="index"></param> /// <param name="paramName"></param> /// <returns></returns> public static string getQuerySegment(string query, urlSegment targetSegment, int index, string paramName = "") { if (string.IsNullOrEmpty(query)) { return(""); } List <string> queryLines = new List <string>(); Dictionary <string, string> queryPairs = new Dictionary <string, string>(); Dictionary <int, KeyValuePair <string, string> > queryIndexedPairs = new Dictionary <int, KeyValuePair <string, string> >(); queryLines.AddRange(query.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); foreach (string qline in queryLines) { string[] tmpArr = qline.Split("=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int tmpIndex = queryLines.IndexOf(qline); if (tmpArr.Length > 0) { queryPairs.Add(tmpArr[0], tmpArr[1]); queryIndexedPairs.Add(tmpIndex, new KeyValuePair <string, string>(tmpArr[0], tmpArr[1])); } } string paramValue = ""; if (!(paramName == "")) { if (queryPairs.ContainsKey(paramName)) { paramValue = queryPairs[paramName]; } } if (index < 0) { return(""); } if (index >= queryLines.Count) { return(""); } if (queryIndexedPairs.ContainsKey(index)) { switch (targetSegment) { case urlSegment.getQParamName: return(queryIndexedPairs[index].Key); // break; case urlSegment.getQParamValue: if (!(paramValue == "")) { return(paramValue); } return(queryIndexedPairs[index].Value); // break; case urlSegment.getQParamSegment: return(queryIndexedPairs[index].Key + "=" + queryIndexedPairs[index].Value); // break; } } return(""); }
/// <summary> /// Vraca segment URL-a /// </summary> /// <param name="input"></param> /// <param name="targetSegment"></param> /// <param name="indInput"></param> /// <returns></returns> public static string getUrlSegment(this string input, urlSegment targetSegment, object indInput = null) { string output = ""; Uri tmpUri = null; bool haveQuery = false; int index = 0; string paramName = ""; if (indInput == null) { index = 0; } else { if (typeof(string) == indInput.GetType()) { paramName = indInput as string; } } haveQuery = (input.IndexOf('?') > -1); if (haveQuery) { string[] tmpArr = input.Split("?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (tmpArr.Length > 1) { string query = tmpArr[1]; switch (targetSegment) { case urlSegment.getQParamName: case urlSegment.getQParamSegment: case urlSegment.getQParamValue: output = getQuerySegment(query, targetSegment, index, paramName); break; } } } switch (targetSegment) { case urlSegment.getFileName: output = tmpUri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped); break; case urlSegment.getFileExtension: output = Path.GetExtension(input); break; case urlSegment.getFileNameOnly: output = Path.GetFileNameWithoutExtension(input); break; case urlSegment.getPathBeforeFileName: output = Path.GetDirectoryName(input); break; default: break; } return(output); }