示例#1
0
        /// <summary>
        /// Converts a virtual (relative) path to an application absolute path.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string Content(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            //if this is a protocol-relative/protocol-less uri, then we need to add the protocol for the remaining
            // logic to work properly
            if (path.StartsWith("//"))
            {
                var scheme = _siteInfo.GetBaseUrl().Scheme;
                return(Regex.Replace(path, @"^\/\/", string.Format("{0}{1}", scheme, Constants.SchemeDelimiter)));
            }

            //This code is taken from the UrlHelper code ... which shouldn't need to be tucked away in there
            // since it is not dependent on the ActionContext
            if (path[0] == 126)
            {
                PathString pathBase = _siteInfo.GetBasePath();
                return(pathBase.Add(new PathString(path.Substring(1))).Value);
            }

            return(path);
        }
        /// <summary>
        /// Formats a file path into a compatible path for use with the file provider
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <remarks>
        /// This will handle virtual paths like ~/myfile.js
        /// This will handle absolute paths like /myfile.js
        /// This will handle absolute paths like /myvirtualapp/myfile.js - where myvirtualapp is a virtual application (Path Base)
        /// </remarks>
        public string ConvertToFileProviderPath(string path)
        {
#if NETCORE3_0
            if (path.StartsWith('~'))
#else
            if (path.StartsWith("~"))
#endif
            {
                return(path.TrimStart('~'));
            }


            string pathBase = _siteInfo.GetBasePath();

            if (pathBase.Length > 0 && path.StartsWith(pathBase))
            {
                return(path.Substring(pathBase.Length));
            }

            return(path);
        }