Asp.Net 之 当前上下文中不存在名称" Server "

  在开发中经常用到应用程序的物理路径,在获取应用程序中文件的物理路径时最常用:

  string path = Server.MapPath("Document/example.doc");

  这里用Server.MapPath得到应用程序的物理路径。如果你是在当前 web 项目下的一个类中如上写链接语句,可能会提示找不到Server,因为 Server的完整路径是System.Web.HttpContext.Current.Server。using System.Web是显然不够的,但是在从内置 Page 类继承的类中可以直接写Server.MapPath("Document/example.doc"),应该是因为 Page 类已经包含了这些类路径。
  如果你从 Page 类继承的类中执行这条语句,才可以简单地使用

  string path= Server.MapPath("Document/example.doc");
  否则写全命名空间:string path = System.Web.HttpContext.Current.Server.MapPath("Document/example.doc");
  

  注意:如果是在一个类库下的一个类中、要先添加引入using System.Web,因为新建一个类库时默认不引入 using System.Web。

原文地址:https://www.cnblogs.com/xinaixia/p/4866065.html