zoukankan      html  css  js  c++  java
  • Server.MapPath() 转

    1.  Server.MapPath()介绍

    Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文件路径。其参数path为Web 服务器的虚拟路径,返回结果是与path相对应的物理文件路径。但有时参数并非为虚拟路径,

    而是用户自定义的文件名。

    Server.MapPath()的全名是System.Web.HttpContext.Current.Server.MapPath()。有时在程序调试时会提示“当前上下文中不存在名称“Server””错误,从而不支持函数Server.MapPath()的使用。尽管

    引用了命名空间“using System.Web;”也是无济于事,此时就需要使用其全名,或者是当前使用Server.MapPath()函数的类继承自System.Web.UI.Page。

    2.  Server.MapPath()应用

    Server.MapPath("") :返回当前页面所在的物理文件路径

    Server.MapPath("/") :返回应用程序根目录所在的物理文件路径

    Server.MapPath("./") :返回当前页面所在的物理文件路径Server.MapPath("../"):返回当前页面所在的上一级的物理文件路径Server.MapPath("~/"):返回应用程序的虚拟目录(路径)

    Server.MapPath("~"):返回应用程序的虚拟目录(路径)

    3.      说明:对于Server.MapPath()具体返回什么内容,在不同的环境下得到的结果也许并不相同。正如上面的六个Server.MapPath()应用,我在VS2010中测试时,其中第二项、第四项会提示错误“未能映射路

    径”,但是改变程序所在的位置时,仅第二项会提示错误。故不同的软件、不同的环境对其支持度也不同。

    如果新建类库项目,在类里面输入Server.Mappath();错误,只要当前类继承一下System.Web.UI.Page类就行了。

    public class TagProgram : System.Web.UI.Page
    {

           //获取Alien的IP地址和端口
            private string[] getAlienInfo()
            {
                string [] strResult = null;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Server.MapPath("AlienConfig.xml"));

         。。。

            }

    }

  • 相关阅读:
    Asp.net MVC 3 RTM 源代码中单元测试帮助类
    CSharp扩展方法应用之获取特性
    Asp.net MVC中防止HttpPost重复提交
    JQuery实现倒计划按钮
    JQuery防止退格键网页后退
    .net中用Action等委托向外传递参数
    linux shell 用sed命令在文本的行尾或行首添加字符
    MongoDB分片中片键的选择
    Mongodb的Replica Sets + Sharding架构
    Mongodb数据分片的维护
  • 原文地址:https://www.cnblogs.com/zqn518/p/3106083.html
Copyright © 2011-2022 走看看