ScrewTurn.Wiki.UrlTools.GetCurrentNamespace C# (CSharp) Method

GetCurrentNamespace() public static method

Extracts the current namespace from the URL, such as /App/Namespace.Edit.aspx.
public static GetCurrentNamespace ( ) : string
return string
        public static string GetCurrentNamespace()
        {
            string filename = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Path); // e.g. MainPage or Edit or Namespace.MainPage or Namespace.Edit

            // Use dot to split the filename
            string[] fields = filename.Split('.');

            if(fields.Length != 1 && fields.Length != 2) return null; // Unrecognized format
            if(fields.Length == 1) return ""; // Just page name
            else return fields[0]; // Namespace.Page
        }

Usage Example

示例#1
0
        /// <summary>
        /// Properly routes the current virtual request to a physical ASP.NET page.
        /// </summary>
        public static void RouteCurrentRequest( )
        {
            string physicalPath;

            try
            {
                physicalPath = HttpContext.Current.Request.PhysicalPath;
            }
            catch (ArgumentException)
            {
                // Illegal characters in path
                HttpContext.Current.Response.Redirect("~/PageNotFound.aspx");
                return;
            }

            // Extract the physical page name, e.g. MainPage, Edit or Category
            string pageName = Path.GetFileNameWithoutExtension(physicalPath);
            // Exctract the extension, e.g. .ashx or .aspx
            var extension = Path.GetExtension(HttpContext.Current.Request.PhysicalPath);

            if (extension == null)
            {
                //If extension null, nothing to do.
                return;
            }
            string ext = extension.ToLowerInvariant( );

            // Remove trailing dot, .ashx -> ashx
            if (ext.Length > 0)
            {
                ext = ext.Substring(1);
            }

            // IIS7+Integrated Pipeline handles all requests through the ASP.NET engine
            // All non-interesting files are not processed, such as GIF, CSS, etc.
            if (ext != "ashx" && ext != "aspx")
            {
                return;
            }

            // Extract the current namespace, if any
            string nspace = UrlTools.GetCurrentNamespace( ) + "";

            if (!string.IsNullOrEmpty(nspace))
            {
                // Verify that namespace exists
                if (Pages.FindNamespace(nspace) == null)
                {
                    HttpContext.Current.Response.Redirect("~/PageNotFound.aspx?Page=" + pageName);
                }
            }
            // Trim Namespace. from pageName
            if (!string.IsNullOrEmpty(nspace))
            {
                pageName = pageName.Substring(nspace.Length + 1);
            }

            string queryString = "";             // Empty or begins with ampersand, not question mark

            try
            {
                // This might throw exceptions if 3rd-party modules interfer with the request pipeline
                queryString = HttpContext.Current.Request.Url.Query.Replace("?", "&");                   // Host not used
            }
            catch { }

            if (ext.Equals("ashx"))
            {
                // Content page requested, process it via Default.aspx
                if (!queryString.Contains("NS="))
                {
                    HttpContext.Current.RewritePath("~/Default.aspx?Page=" + Tools.UrlEncode(pageName) + "&NS=" + Tools.UrlEncode(nspace) + queryString);
                }
                else
                {
                    HttpContext.Current.RewritePath("~/Default.aspx?Page=" + Tools.UrlEncode(pageName) + queryString);
                }
            }
            else if (ext.Equals("aspx"))
            {
                // System page requested, redirect to the root of the application
                // For example: http://www.server.com/Namespace.Edit.aspx?Page=MainPage -> http://www.server.com/Edit.aspx?Page=MainPage&NS=Namespace
                if (!string.IsNullOrEmpty(nspace))
                {
                    if (!queryString.Contains("NS="))
                    {
                        HttpContext.Current.RewritePath("~/" + Tools.UrlEncode(pageName) + "." + ext + "?NS=" + Tools.UrlEncode(nspace) + queryString);
                    }
                    else
                    {
                        if (queryString.Length > 1)
                        {
                            queryString = "?" + queryString.Substring(1);
                        }
                        HttpContext.Current.RewritePath("~/" + Tools.UrlEncode(pageName) + "." + ext + queryString);
                    }
                }
            }
            // else nothing to do
        }