关于Server.MapPath和HostingEnvironment.MapPath

在ASP.NET项目中,当需要在Controller里重写static方法时,使用Server.MapPath会遇到对象引用错误。文章详细解释了如何使用HostingEnvironment.MapPath作为替代方案,并对比了两者在处理null和相对路径上的不同。

 最近做的项目需要在Controller里重写一个static的方法,在方法内用常用的Server.MapPath会报一个错误:An object reference is required for the non-static field, method, or property 'Controller.Server'。从错误可以看出来,Server.MapPath并不是一个static方法,所以使用HostingEnvironment的MapPath方法作为替代,这是一个static的方法。

查了下,Server.MapPath实际上最终还是调用了HostingEnvironment.MapPath,但他俩也不是完全的相等,存在一些区别,Server.MapPath用创建了一个VirtualPath的object。


VirtualPath.Create(path, VirtualPathOptions.AllowAllPath|VirtualPathOptions.AllowNull);
所以Server.MapPath相对来说增加了一些功能,对比如下:

1、Server.MapPath接受null,HostingEnvironment.MapPath不可以

2、HostingEnvironment.MapPath不接受相对路径

因为Server.MapPath需要HttpContext,所以大多数时候都可以直接用HostingEnvironment.MapPath,除非会出现以上两种情况。目前测试出来的就这两种,其他的情况还需要后续验证。
--------------------- 
作者:xrjohn 
来源:优快云 
原文:https://blog.youkuaiyun.com/xrjohn/article/details/50952503 
版权声明:本文为博主原创文章,转载请附上博文链接!

/// <summary> /// 版本更新 /// </summary> /// <param name="VersionCode">手机中已安装的版本号</param> /// <returns></returns> [CustomUnAuthorize] public ActionResult VersionUpdate(string VersionCode) { var result = string.Empty; string url = Server.MapPath("~"); url = "{0}{1}".FormatTo(url, "\\Content\\APPInstall"); var appFiles = Directory.GetFiles(url, "Suncere_YiChun_*.apk"); if (appFiles.Length == 0) { return Content(result); } Dictionary<decimal, string> apkVersionCodes = new Dictionary<decimal, string>(); foreach (var appFile in appFiles) { var fullName = appFile.Split('\\').LastOrDefault(); var newVersion = Path.GetFileNameWithoutExtension(fullName).Replace("Suncere_YiChun_", ""); var newV = decimal.Parse(newVersion.Replace(".", "")); apkVersionCodes[newV] = newVersion; } var maxCodeValue = apkVersionCodes.Max(x => x.Key); var oldV = decimal.Parse(VersionCode.Replace(".", "")); if (oldV >= maxCodeValue) { return Content(result); } string uri = HttpContext.Request.Url.Authority; result = "\"http://{0}/Content/APPInstall/{1}\"".FormatTo(uri, string.Format("Suncere_YiChun_{0}.apk", apkVersionCodes[maxCodeValue])); return Content(result); } 将 上面 MVC接口改为AppService 接口 [HttpGet] [Route("VersionUpdate")] public string VersionUpdate(string VersionCode) { var result = string.Empty; string url = Server.MapPath("~"); url = "{0}{1}".FormatTo(url, "\\Content\\APPInstall"); var appFiles = Directory.GetFiles(url, "Suncere_YiChun_*.apk"); if (appFiles.Length == 0) { return result; } Dictionary<decimal, string> apkVersionCodes = new Dictionary<decimal, string>(); foreach (var appFile in appFiles) { var fullName = appFile.Split('\\').LastOrDefault(); var newVersion = Path.GetFileNameWithoutExtension(fullName).Replace("Suncere_YiChun_", ""); var newV = decimal.Parse(newVersion.Replace(".", "")); apkVersionCodes[newV] = newVersion; } var maxCodeValue = apkVersionCodes.Max(x => x.Key); var oldV = decimal.Parse(VersionCode.Replace(".", "")); if (oldV >= maxCodeValue) { return result; } string uri = HttpContext.Request.Url.Authority; result = "\"http://{0}/Content/APPInstall/{1}\"".FormatTo(uri, string.Format("Suncere_YiChun_{0}.apk", apkVersionCodes[maxCodeValue])); return result; }
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值