ASP.NET Core-获取Server对象

实现一个.net framework中Server功能

ConfigureWebServices(this IServiceCollection services){
services.AddSingleton(typeof(HttpServerUtilityBase));
}
    /// <summary>
    /// https://stackoverflow.com/questions/43992261/how-to-get-absolute-path-in-asp-net-core-alternative-way-for-server-mappath
    /// </summary>
    public class HttpServerUtilityBase
    {
        private IWebHostEnvironment _webhostEnvironment;

        public HttpServerUtilityBase(IWebHostEnvironment webhostEnvironment)
        {
            this._webhostEnvironment = webhostEnvironment;
        }
        /// <summary>
        /// 实现DotNet FrameWork里面的MapPath
        /// </summary>
        /// <param name="path">兼容~/和/开头的绝对路径</param>
        /// <returns></returns>
        public string MapPath(string path)
        {
            string rootPath = this._webhostEnvironment.WebRootPath;
            if (string.IsNullOrWhiteSpace(path))
            {
                return rootPath;
            }
            if (path.StartsWith("~/"))
            {
                path = path.Substring(2);
            }
            if (path.StartsWith("/"))
            {
                path = path.Substring(1);
            }
            return Path.Combine(rootPath, path.Replace('/', Path.DirectorySeparatorChar));
        }

        /// <summary>
        /// https://stackoverflow.com/questions/35437491/webutility-htmldecode-replacement-in-net-core
        /// </summary>
        public string HtmlEncode(string value)
        {
            return System.Web.HttpUtility.HtmlEncode(value);
            //https://edi.wang/post/2018/11/25/netcore-webutility-urlencode-httputility-urlencode
            //return System.Net.WebUtility.HtmlEncode(value);
        }

        public string HtmlDecode(string value)
        {
            return System.Web.HttpUtility.HtmlDecode(value);
        }

        public string UrlEncode(string value)
        {
            return System.Web.HttpUtility.UrlEncode(value);
        }

        public string UrlDecode(string value)
        {
            return System.Web.HttpUtility.UrlDecode(value);
        }

        public string Base64UrlEncode(string text, Encoding encoding)
        {
            byte[] bytes = encoding.GetBytes(text);
            return Base64UrlEncode(bytes);
        }

        public string Base64UrlEncode(byte[] bytes)
        {
            return Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Encode(bytes);
        }

        public string Base64UrlDecode(string text, Encoding encoding)
        {
            byte[] bytes = Base64UrlDecode(text);
            return encoding.GetString(bytes);
        }

        public byte[] Base64UrlDecode(string text)
        {
            return Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Decode(text);
        }
        /// <summary>
        /// 生成带有参数的Url
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="queryString"></param>
        /// <returns>URL编码之后的结果</returns>
        public string AddQueryString(string uri, IDictionary<string, string> queryString)
        {
            return Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(uri, queryString);
        }
        /// <summary>
        /// 将Url里面的参数取出来变成键值对格式的
        /// </summary>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public IDictionary<string, Microsoft.Extensions.Primitives.StringValues> ParseNullableQuery(string queryString)
        {
            return Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseNullableQuery(queryString);
        }
    }
原文地址:https://www.cnblogs.com/fanfan-90/p/13322573.html