public void Simple_URL_Parameter_Parser_Test() { var parser = new SimpleURLParameterParser("g=forum&t=1&url=&pg=43#cool"); Assert.AreEqual(true, parser.HasAnchor, "Url should have an Anchor"); Assert.AreEqual("cool", parser.Anchor, "The Url should be called : cool"); Assert.AreEqual("forum", parser.Parameters["g"], "The Parameter g should equals forum"); Assert.AreEqual("url=&pg=43", parser.CreateQueryString(new[] { "g", "t" }), "The Parameter g should equals forum"); Assert.AreEqual("43", parser.Parameters[3], "The Parameter Index 3 should be 43"); }
/// <summary> /// Build the url. /// </summary> /// <param name="url">The url.</param> /// <returns> /// Returns the URL. /// </returns> public override string BuildUrl(string url) { string newUrl = "{0}{1}?{2}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName, url); // create scriptName string scriptName = "{0}{1}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName); // get the base script file from the config -- defaults to, well, default.aspx :) string scriptFile = Config.BaseScriptFile; if (url.IsNotSet()) { return newUrl; } if (scriptName.EndsWith(scriptFile)) { var before = scriptName.Remove(scriptName.LastIndexOf(scriptFile, StringComparison.Ordinal)); var parser = new SimpleURLParameterParser(url); // create "rewritten" url... newUrl = "{0}{1}".FormatWith(before, Config.UrlRewritingPrefix); var useKey = string.Empty; var description = string.Empty; var pageName = parser["g"]; ForumPages forumPage = ForumPages.forum; var getDescription = false; var isFeed = false; var handlePage = false; if (pageName.IsSet()) { try { forumPage = pageName.ToEnum<ForumPages>(); getDescription = true; } catch (Exception) { getDescription = false; } } if (getDescription) { switch (forumPage) { case ForumPages.topics: useKey = "f"; description = UrlRewriteHelper.GetForumName(parser[useKey].ToType<int>()); handlePage = true; break; case ForumPages.posts: if (parser["t"].IsSet()) { useKey = "t"; pageName += "t"; description = UrlRewriteHelper.GetTopicName(parser[useKey].ToType<int>()); } else if (parser["m"].IsSet()) { useKey = "m"; pageName += "m"; try { description = UrlRewriteHelper.GetTopicNameFromMessage(parser[useKey].ToType<int>()); } catch (Exception) { description = "posts"; } } handlePage = true; break; case ForumPages.profile: useKey = "u"; description = UrlRewriteHelper.CleanStringForURL( parser["name"].IsSet() ? parser["name"] : UrlRewriteHelper.GetProfileName(parser[useKey].ToType<int>())); parser.Parameters.Remove("name"); break; case ForumPages.forum: if (parser["c"].IsSet()) { useKey = "c"; description = UrlRewriteHelper.GetCategoryName(parser[useKey].ToType<int>()); } break; case ForumPages.rsstopic: if (parser["pg"].IsSet()) { description = parser["pg"].ToEnum<YafRssFeeds>().ToString().ToLower(); } if (parser["f"].IsSet()) { description += "_{0}".FormatWith(UrlRewriteHelper.GetForumName(parser["f"].ToType<int>())); } if (parser["t"].IsSet()) { description += "_{0}".FormatWith(UrlRewriteHelper.GetTopicName(parser["t"].ToType<int>())); } if (parser["c"].IsSet()) { description += "_{0}".FormatWith(UrlRewriteHelper.GetCategoryName(parser["c"].ToType<int>())); } if (parser["ft"].IsSet()) { description += parser["ft"].ToType<int>() == YafSyndicationFormats.Atom.ToInt() ? "-atom" : "-rss"; } handlePage = true; isFeed = true; break; } } newUrl += pageName; if (useKey.Length > 0) { newUrl += parser[useKey]; } if (handlePage && parser["p"] != null && !isFeed) { int page = parser["p"].ToType<int>(); if (page != 1) { newUrl += "p{0}".FormatWith(page); } parser.Parameters.Remove("p"); } if (isFeed) { if (parser["ft"] != null) { int page = parser["ft"].ToType<int>(); newUrl += "ft{0}".FormatWith(page); parser.Parameters.Remove("ft"); } if (parser["f"] != null) { int page = parser["f"].ToType<int>(); newUrl += "f{0}".FormatWith(page); parser.Parameters.Remove("f"); } if (parser["t"] != null) { int page = parser["t"].ToType<int>(); newUrl += "t{0}".FormatWith(page); parser.Parameters.Remove("t"); } } if (parser["find"] != null) { newUrl += "find{0}".FormatWith(parser["find"].Trim()); parser.Parameters.Remove("find"); } if (description.Length > 0) { if (description.EndsWith("-")) { description = description.Remove(description.Length - 1, 1); } newUrl += "_{0}".FormatWith(description); } newUrl += ".aspx"; var restURL = parser.CreateQueryString(new[] { "g", useKey }); // append to the url if there are additional (unsupported) parameters if (restURL.Length > 0) { newUrl += "?{0}".FormatWith(restURL); } // see if we can just use the default (/) if (newUrl.EndsWith("{0}forum.aspx".FormatWith(Config.UrlRewritingPrefix))) { // remove in favor of just slash... newUrl = newUrl.Remove( newUrl.LastIndexOf( "{0}forum.aspx".FormatWith(Config.UrlRewritingPrefix), StringComparison.Ordinal), "{0}forum.aspx".FormatWith(Config.UrlRewritingPrefix).Length); } // add anchor if (parser.HasAnchor) { newUrl += "#{0}".FormatWith(parser.Anchor); } } // just make sure & is & ... newUrl = newUrl.Replace("&", "&").Replace("&", "&"); return newUrl; }
/// <summary> /// Build the url. /// </summary> /// <param name="url">The url.</param> /// <returns> /// Returns the URL. /// </returns> public override string BuildUrl(string url) { string newUrl = "{0}{1}?{2}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName, url); // create scriptName string scriptName = "{0}{1}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName); // get the base script file from the config -- defaults to, well, default.aspx :) string scriptFile = Config.BaseScriptFile; if (url.IsNotSet()) { return newUrl; } if (scriptName.EndsWith(scriptFile)) { string before = scriptName.Remove(scriptName.LastIndexOf(scriptFile, StringComparison.Ordinal)); var parser = new SimpleURLParameterParser(url); // create "rewritten" url... newUrl = before; var useKey = string.Empty; var description = string.Empty; var pageName = parser["g"]; bool isFeed = false; bool handlePage = false; switch (pageName) { case "topics": useKey = "f"; pageName += "/"; description = UrlRewriteHelper.GetForumName(parser[useKey].ToType<int>()); handlePage = true; break; case "posts": pageName += "/"; if (parser["t"].IsSet()) { useKey = "t"; pageName += "t"; description = UrlRewriteHelper.GetTopicName(parser[useKey].ToType<int>()); } else if (parser["m"].IsSet()) { useKey = "m"; pageName += "m"; try { description = UrlRewriteHelper.GetTopicNameFromMessage(parser[useKey].ToType<int>()); } catch (Exception) { description = "posts"; } } handlePage = true; break; case "profile": useKey = "u"; pageName += "/"; description = UrlRewriteHelper.GetProfileName(parser[useKey].ToType<int>()); break; case "forum": pageName = "category/"; if (parser["c"].IsSet()) { useKey = "c"; description = UrlRewriteHelper.GetCategoryName(parser[useKey].ToType<int>()); } else { pageName = string.Empty; } break; case "rsstopic": pageName += "/"; if (parser["pg"].IsSet()) { description = parser["pg"].ToEnum<YafRssFeeds>().ToString().ToLower(); } if (parser["f"].IsSet()) { description += UrlRewriteHelper.GetForumName(parser["f"].ToType<int>()); } if (parser["t"].IsSet()) { description += UrlRewriteHelper.GetTopicName(parser["t"].ToType<int>()); } if (parser["ft"].IsSet()) { description += parser["ft"].ToType<int>() == YafSyndicationFormats.Atom.ToInt() ? "-atom" : "-rss"; } handlePage = true; isFeed = true; break; } // special handling for admin pages if (parser["g"].StartsWith("admin_")) { pageName = parser["g"].Replace("_", "/"); } newUrl += pageName; if (useKey.Length > 0) { newUrl += parser[useKey]; } if (handlePage && parser["p"] != null && !isFeed) { int page = parser["p"].ToType<int>(); if (page != 1) { newUrl += "p{0}".FormatWith(page); } parser.Parameters.Remove("p"); } if (isFeed) { if (parser["ft"] != null) { int page = parser["ft"].ToType<int>(); newUrl += "ft{0}".FormatWith(page); parser.Parameters.Remove("ft"); } if (parser["f"] != null) { int page = parser["f"].ToType<int>(); newUrl += "f{0}".FormatWith(page); parser.Parameters.Remove("f"); } if (parser["t"] != null) { int page = parser["t"].ToType<int>(); newUrl += "t{0}".FormatWith(page); parser.Parameters.Remove("t"); } } if (parser["find"] != null) { newUrl += "find{0}".FormatWith(parser["find"].Trim()); parser.Parameters.Remove("find"); } if (description.Length > 0) { if (description.EndsWith("-")) { description = description.Remove(description.Length - 1, 1); } newUrl += "-{0}".FormatWith(description); } var restURL = parser.CreateQueryString(new[] { "g", useKey }); // append to the url if there are additional (unsupported) parameters if (restURL.Length > 0) { newUrl += "?{0}".FormatWith(restURL); } if (newUrl.EndsWith("/forum")) { // remove in favor of just slash... newUrl = newUrl.Remove( newUrl.LastIndexOf( "/forum", StringComparison.Ordinal), "/forum".Length); } // add anchor if (parser.HasAnchor) { newUrl += "#{0}".FormatWith(parser.Anchor); } } // just make sure & is & ... newUrl = newUrl.Replace("&", "&").Replace("&", "&"); return newUrl; }
/// <summary> /// Gets the page URL. /// </summary> /// <param name="page">The page.</param> /// <returns> /// The get page url. /// </returns> protected string GetPageURL(int page) { string url; // create proper query string... var parser = new SimpleURLParameterParser(this.Get<HttpRequestBase>().QueryString.ToString()); // get the current page var currentPage = (ForumPages)Enum.Parse(typeof(ForumPages), parser["g"], true); if (parser["m"] != null) { // must be converted to by topic... parser.Parameters.Remove("m"); parser.Parameters.Add("t", YafContext.Current.PageTopicID.ToString()); } if (page > 1) { string tmp = parser.CreateQueryString(new[] { "g", "p", "tabid", "find" }); if (tmp.Length > 0) { tmp += "&"; } tmp += "p={0}"; url = YafBuildLink.GetLink(currentPage, tmp, page); } else { url = YafBuildLink.GetLink(currentPage, parser.CreateQueryString(new[] { "g", "p", "tabid", "find" })); } return url; }
/// <summary> /// The build url. /// </summary> /// <param name="url"> /// The url. /// </param> /// <returns> /// The build url. /// </returns> public override string BuildUrl(string url) { string newUrl = "{0}{1}?{2}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName, url); // create scriptName string scriptName = "{0}{1}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName); // get the base script file from the config -- defaults to, well, default.aspx :) string scriptFile = Config.BaseScriptFile; if (scriptName.EndsWith(scriptFile)) { string before = scriptName.Remove(scriptName.LastIndexOf(scriptFile)); var parser = new SimpleURLParameterParser(url); // create "rewritten" url... newUrl = before + Config.UrlRewritingPrefix; string useKey = string.Empty; string useKey2 = string.Empty; string description = string.Empty; string pageName = parser["g"]; bool isFeed = false; // const bool showKey = false; bool handlePage = false; switch (parser["g"]) { case "topics": useKey = "f"; description = this.GetForumName(parser[useKey].ToType<int>()); handlePage = true; break; case "posts": if (parser["t"].IsSet()) { useKey = "t"; pageName += "t"; description = this.GetTopicName(parser[useKey].ToType<int>()); } else if (parser["m"].IsSet()) { useKey = "m"; pageName += "m"; try { description = this.GetTopicNameFromMessage(parser[useKey].ToType<int>()); } catch (Exception) { description = "posts"; } } handlePage = true; break; case "profile": useKey = "u"; // description = GetProfileName( Convert.ToInt32( parser [useKey] ) ); break; case "forum": if (parser["c"].IsSet()) { useKey = "c"; description = this.GetCategoryName(parser[useKey].ToType<int>()); } break; case "rsstopic": if (parser["pg"].IsSet()) { useKey = "pg"; // pageName += "pg"; if (parser[useKey].ToType<int>() == YafRssFeeds.Active.ToInt()) { description = "active"; } if (parser[useKey].ToType<int>() == YafRssFeeds.Favorite.ToInt()) { description = "favorite"; } if (parser[useKey].ToType<int>() == YafRssFeeds.Forum.ToInt()) { description = "forum"; } if (parser[useKey].ToType<int>() == YafRssFeeds.LatestAnnouncements.ToInt()) { description = "latestannouncements"; } if (parser[useKey].ToType<int>() == YafRssFeeds.LatestPosts.ToInt()) { description = "latestposts"; } if (parser[useKey].ToType<int>() == YafRssFeeds.Posts.ToInt()) { description = "posts"; } if (parser[useKey].ToType<int>() == YafRssFeeds.Topics.ToInt()) { description = "topics"; } } if (parser["f"].IsSet()) { useKey2 = "f"; description += this.GetForumName(parser[useKey2].ToType<int>()); } if (parser["t"].IsSet()) { useKey2 = "t"; description += this.GetTopicName(parser[useKey2].ToType<int>()); } if (parser["ft"].IsSet()) { useKey2 = "ft"; if ((parser[useKey2].ToType<int>()) == YafSyndicationFormats.Atom.ToInt()) { description += "-atom"; } else { description += "-rss"; } } handlePage = true; isFeed = true; break; } newUrl += pageName; if (useKey.Length > 0) { newUrl += parser[useKey]; } if (handlePage && parser["p"] != null && !isFeed) { int page = parser["p"].ToType<int>(); if (page != 1) { newUrl += "p{0}".FormatWith(page); } parser.Parameters.Remove("p"); } if (isFeed) { if (parser["ft"] != null) { int page = parser["ft"].ToType<int>(); newUrl += "ft{0}".FormatWith(page); parser.Parameters.Remove("ft"); } if (parser["f"] != null) { int page = parser["f"].ToType<int>(); newUrl += "f{0}".FormatWith(page); parser.Parameters.Remove("f"); } if (parser["t"] != null) { int page = parser["t"].ToType<int>(); newUrl += "t{0}".FormatWith(page); parser.Parameters.Remove("t"); } } if (parser["find"] != null) { newUrl += "find{0}".FormatWith(parser["find"].Trim()); parser.Parameters.Remove("find"); } if (description.Length > 0) { if (description.EndsWith("-")) { description = description.Remove(description.Length - 1, 1); } newUrl += "_{0}".FormatWith(description); } if (!isFeed) { newUrl += ".aspx"; } else { newUrl += ".xml"; } string restURL = parser.CreateQueryString(new[] { "g", useKey }); // append to the url if there are additional (unsupported) parameters if (restURL.Length > 0) { newUrl += "?{0}".FormatWith(restURL); } // see if we can just use the default (/) if (newUrl.EndsWith("yaf_forum.aspx")) { // remove in favor of just slash... newUrl = newUrl.Remove(newUrl.LastIndexOf("yaf_forum.aspx"), "yaf_forum.aspx".Length); } // add anchor if (parser.HasAnchor) { newUrl += "#{0}".FormatWith(parser.Anchor); } } // just make sure & is & ... newUrl = newUrl.Replace("&", "&").Replace("&", "&"); return newUrl; }