zoukankan      html  css  js  c++  java
  • Server.MapPath查询路径那几件事

    主要总结Server.MapPath 这个方法的使用以及使用的场景,不是什么时候都适合使用;

    1、实现功能:

      Server.MapPath能够获取指定URL相对服务器的物理路径,在IIS服务端,能够根据文件名来获取该文件的物理路径;

    2、存在命令空间:

      System.Web.HttpContext.Current.Server.MapPath 以及System.web.MVC.Control.Server.Mapth;

    3、使用情况:

      既然是System.Web.HttpContent 也及时表明该方法只能放在Http.web中使用,非该环境系统会扔出一个错误;非web环境是什么意思那,举个例子,我们使用线程来处理某个业务逻辑的时候,这个时候你使用该方法,那必然报错,以为你已经脱离了web环境。所以视情况而定;获取虚拟目录的物理地址,该方法很有效果;

      随便补充一句,多线程编程的时候,一定要分清楚那些事线程能够获取的资源,那些事依赖其他环境获取的变量,比如IIS中多线程获取缓存数据,离开了HttpWeb这环境来获取IIS的缓存,必然是失败的,所以要分清楚多线程编程时候,使用的资源对象。线程安全对象集合:ConcurrentQueue、ConcurrentBag等

    4、需要注意事项:

        system.Web.HttpContext.Current.Server.MapPath("myPic") 也就是获取当前平级目录地址;

        system.Web.HttpContext.Current.Server.MapPath("../myPic") 也就是获取当前上级目录地址;

           使用的时候需要慎重;

    5、同类使用注意:

    HttpContext.Current.Cache.Add(CacheKey, DataDirectory, null, DateTime.MaxValue, TimeSpan.FromMinutes(15), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemovedCallback));
    
    
    public static void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
            {
                /****S测试压力代码 数据请求*****/
                Dictionary<string, int> DataDic = (Dictionary<string, int>)value;
                string ResultText = "";
                foreach (var currkey in DataDic)
                {
                    ResultText += currkey.Key + "=" + currkey.Value+"$";
                }            
                LogFun.Instance().WriteLog(key, ResultText.Trim('$'),false);
                /****E测试压力代码 数据请求*****/
            }
  • 相关阅读:
    [LeetCode 049] Group Anagrams
    [LeetCode 033] Search in Rotated Sorted Array
    [LeetCode 024] Swap Nodes in Pairs
    [LeetCode 016] 3Sum Closest
    [LeetCode 015] 3Sum
    [LeetCode 013] Roman to Integer
    [LeetCode 008] String to Integer (atoi)
    [LeetCode 007] Reverse Integer
    第四课:文件操作【解密】
    第三课:文件操作【逐步浅入,深入加解法】
  • 原文地址:https://www.cnblogs.com/xibei666/p/5625351.html
Copyright © 2011-2022 走看看