无法使用Server.MapPath

时间:2012-06-19 16:59:33

标签: c# visual-studio intellisense server.mappath

要使Server.MapPath工作,我必须做些什么? 我有using System.Web;

还有什么?当我输入Server时,Server没有快速结果选项(智能感知)。

任何帮助?

9 个答案:

答案 0 :(得分:265)

您可以尝试使用此

    System.Web.HttpContext.Current.Server.MapPath(path);

或使用HostingEnvironment.MapPath

    System.Web.Hosting.HostingEnvironment.MapPath(path);

答案 1 :(得分:14)

您的项目需要引用程序集System.Web.dll。服务器是HttpServerUtility类型的对象。例如:

HttpContext.Current.Server.MapPath(path);

答案 2 :(得分:3)

System.Web.HttpContext.Current.Server.MapPath("~/")如果从线程调用它,则为null。

所以,尝试使用

System.Web.Hosting.HostingEnvironment.MapPath("~/")

答案 3 :(得分:1)

如果您没有,请添加对System.web的引用。在参考文件夹中执行此操作。

然后,您可以使用Hosting.HostingEnvironment.MapPath(path);

答案 4 :(得分:1)

bool IsExist = System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/UploadedFiles/"));
if (!IsExist)
    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/UploadedFiles/"));

StreamWriter textWriter = File.CreateText(Path.Combine(HttpContext.Current.Server.MapPath("/UploadedFiles/") + "FileName.csv"));
var csvWriter = new CsvWriter(textWriter, System.Globalization.CultureInfo.CurrentCulture);
csvWriter.WriteRecords(classVM);

答案 5 :(得分:0)

尝试添加System.Web作为项目的参考。

答案 6 :(得分:0)

您需要添加引用(System.WebReference to System.Web

答案 7 :(得分:0)

我知道这篇文章已有几年历史了,但是我要做的是将此行添加到班级顶部,您仍然可以使用Server.MapPath

Dim Server = HttpContext.Current.Server

或者你可以做一个函数

Public Function MapPath(sPath as String)
    return HttpContext.Current.Server.MapPath(sPath)
End Function

我就是要让事情变得更容易。我还把它添加到了Utilities类中,以防万一我再次遇到这个问题。

答案 8 :(得分:0)

我遇到了同样的问题,我想这可能会对某人有所帮助。正如这个问题的原始海报所问

<块引用>

我必须做什么才能使 Server.MapPath 工作?
我使用 System.Web;

我们编写的类应该实现System.Web.UI.Page

比如说,我们的类名是MyClass

public class MyClass: System.Web.UI.Page
{
// Code runs here 
}