コード例 #1
0
        /// <summary>
        /// Build the url.
        /// </summary>
        /// <param name="url">
        /// The url.
        /// </param>
        /// <returns>
        /// Returns the URL.
        /// </returns>
        public override string BuildUrl(string url)
        {
            var newUrl = "{0}{1}?{2}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName, url);

            // create scriptName
            var scriptName = "{0}{1}".FormatWith(AppPath, Config.ForceScriptName ?? ScriptName);

            // get the base script file from the config -- defaults to, well, default.aspx :)
            var 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"];
                var 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>());
                        description =
                            UrlRewriteHelper.CleanStringForURL(
                                parser["name"].IsSet()
                                        ? parser["name"]
                                        : 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)
                {
                    var page = parser["p"].ToType <int>();
                    if (page != 1)
                    {
                        newUrl += "p{0}".FormatWith(page);
                    }

                    parser.Parameters.Remove("p");
                }

                if (isFeed)
                {
                    if (parser["ft"] != null)
                    {
                        var page = parser["ft"].ToType <int>();
                        newUrl += "ft{0}".FormatWith(page);
                        parser.Parameters.Remove("ft");
                    }

                    if (parser["f"] != null)
                    {
                        var page = parser["f"].ToType <int>();
                        newUrl += "f{0}".FormatWith(page);
                        parser.Parameters.Remove("f");
                    }

                    if (parser["t"] != null)
                    {
                        var 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, "name" });

                // 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 &amp; ...
            newUrl = newUrl.Replace("&amp;", "&").Replace("&", "&amp;");

            return(newUrl);
        }
コード例 #2
0
        /// <summary>
        /// Build the url.
        /// </summary>
        /// <param name="url">
        /// The url.
        /// </param>
        /// <returns>
        /// Returns the URL.
        /// </returns>
        public override string BuildUrl(string url)
        {
            var newUrl = $"{AppPath}{Config.ForceScriptName ?? ScriptName}?{url}";

            // create scriptName
            var scriptName = $"{AppPath}{Config.ForceScriptName ?? ScriptName}";

            // get the base script file from the config -- defaults to, well, default.aspx :)
            var scriptFile = Config.BaseScriptFile;

            if (url.IsNotSet())
            {
                return(newUrl);
            }

            const string gsr = "getsearchresults";

            scriptName = scriptName.Replace(gsr, scriptFile);
            newUrl     = newUrl.Replace(gsr, scriptFile);

            if (scriptName.EndsWith(scriptFile))
            {
                var 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"];
                var forumPage      = ForumPages.Board;
                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";
                        pageName += "/";

                        // description = UrlRewriteHelper.GetForumName(parser[useKey].ToType<int>());
                        description =
                            UrlRewriteHelper.CleanStringForURL(
                                parser["name"].IsSet()
                                        ? parser["name"]
                                        : UrlRewriteHelper.GetForumName(parser[useKey].ToType <int>()));
                        handlePage = true;
                        break;

                    case ForumPages.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 ForumPages.Profile:
                        useKey      = "u";
                        pageName   += "/";
                        description =
                            UrlRewriteHelper.CleanStringForURL(
                                parser["name"].IsSet()
                                        ? parser["name"]
                                        : UrlRewriteHelper.GetProfileName(parser[useKey].ToType <int>()));

                        parser.Parameters.Remove("name");
                        break;

                    case ForumPages.Board:
                        pageName = "category/";
                        if (parser["c"].IsSet())
                        {
                            useKey      = "c";
                            description = UrlRewriteHelper.GetCategoryName(parser[useKey].ToType <int>());
                        }
                        else
                        {
                            pageName = string.Empty;
                        }

                        break;

                    case ForumPages.RssTopic:
                        pageName += "/";

                        if (parser["feed"].IsSet())
                        {
                            description = parser["feed"].ToEnum <RssFeeds>().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["c"].IsSet())
                        {
                            description += $"_{UrlRewriteHelper.GetCategoryName(parser["c"].ToType<int>())}";
                        }

                        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];
                }

                // handle pager links
                if (handlePage && parser["p"] != null && !isFeed)
                {
                    var page = parser["p"].ToType <int>();

                    if (page != 1)
                    {
                        description += $"/page{page}";
                    }

                    parser.Parameters.Remove("p");
                }

                if (isFeed)
                {
                    if (parser["type"] != null)
                    {
                        var page = parser["type"].ToType <int>();
                        newUrl += $"type{page}";
                        parser.Parameters.Remove("type");
                    }

                    if (parser["feed"] != null)
                    {
                        var page = parser["feed"].ToType <int>();
                        newUrl += $"feed{page}";
                        parser.Parameters.Remove("feed");
                    }

                    if (parser["f"] != null)
                    {
                        var page = parser["f"].ToType <int>();
                        newUrl += $"f{page}";
                        parser.Parameters.Remove("f");
                    }

                    if (parser["t"] != null)
                    {
                        var page = parser["t"].ToType <int>();
                        newUrl += $"t{page}";
                        parser.Parameters.Remove("t");
                    }
                }

                if (parser["find"] != null)
                {
                    newUrl += $"find{parser["find"].Trim()}";
                    parser.Parameters.Remove("find");
                }

                if (description.Length > 0)
                {
                    if (description.EndsWith("-"))
                    {
                        description = description.Remove(description.Length - 1, 1);
                    }

                    newUrl += $"-{description}";
                }

                var restUrl = parser.CreateQueryString(new[] { "g", useKey, "name" });

                // append to the url if there are additional (unsupported) parameters
                if (restUrl.Length > 0)
                {
                    newUrl += $"?{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 += $"#{parser.Anchor}";
                }
            }

            // just make sure & is &amp; ...
            newUrl = newUrl.Replace("&amp;", "&").Replace("&", "&amp;");

            return(newUrl);
        }