Subtext.Framework.Format.UrlFormats.GetBlogSubfolderFromRequest C# (CSharp) Method

GetBlogSubfolderFromRequest() public static method

Parses out the subfolder of the blog from the requested URL. It simply searches for the first "folder" after the host and Request.ApplicationPath.

For example, if a blog is hosted at the virtual directory http://localhost/Subtext.Web/ and request is made for http://localhost/Subtext.Web/, the subfolder name is "" (empty string). Howver, a request for http://localhost/Subtext.Web/MyBlog/ would return "MyBlog" as the subfolder.

Likewise, if a blog is hosted at http://localhost/, a request for http://localhost/MyBlog/ would return "MyBlog" as the subfolder.

public static GetBlogSubfolderFromRequest ( string rawUrl, string applicationPath ) : string
rawUrl string The raw url.
applicationPath string The virtual application name as found in the Request.ApplicationName property.
return string
        public static string GetBlogSubfolderFromRequest(string rawUrl, string applicationPath)
        {
            if(rawUrl == null)
                throw new ArgumentNullException("rawUrl", "The path cannot be null.");

            if(applicationPath == null)
                throw new ArgumentNullException("applicationPath", "The app should not be null.");

            // The {0} represents a potential virtual directory
            string urlPatternFormat = "{0}/(?<app>.*?)/";

            //Remove any / from App.
            string cleanApp = "/" + StripSurroundingSlashes(applicationPath);
            if(cleanApp == "/")
                cleanApp = string.Empty;
            string appRegex = Regex.Escape(cleanApp);

            string urlRegexPattern = string.Format(CultureInfo.InvariantCulture, urlPatternFormat, appRegex);

            Regex urlRegex = new Regex(urlRegexPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Match match = urlRegex.Match(rawUrl);
            if(match.Success)
            {
                return match.Groups["app"].Value;
            }
            else
            {
                return string.Empty;
            }
        }