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);
        }
    }
posted @   .Neterr  阅读(652)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示