C# 不能使用 Server.MapPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:29:48  来源:igfitidea点击:

Cannot use Server.MapPath

c#visual-studiointellisenseserver.mappath

提问by a1204773

What I must do to make Server.MapPathwork?
I have using System.Web;

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

what else? When I type Serverthere is no quick result option (intelli-sense) for Server.

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

Any help?

有什么帮助吗?

采纳答案by DotNetUser

you can try using this

你可以尝试使用这个

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

or use HostingEnvironment.MapPath

或使用 HostingEnvironment.MapPath

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

回答by Leandro Gomide

Your project needs to reference assembly System.Web.dll. Server is an object of type HttpServerUtility. Example:

您的项目需要引用 assembly System.Web.dll。服务器是一个类型的对象HttpServerUtility。例子:

HttpContext.Current.Server.MapPath(path);

回答by jabu.hlong

Try adding System.Webas a reference to your project.

尝试添加System.Web作为对您的项目的引用。

回答by Aaditya Dubey

You need to add reference (System.Web) Reference to System.Web

您需要添加引用 ( System.Web) 引用 System.Web

回答by jdisla

Firt add a reference to System.web, if you don't have. Do that in the Referencesfolder.

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

You can then use Hosting.HostingEnvironment.MapPath(path);

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

回答by Ravindra Singh Chhabra

System.Web.HttpContext.Current.Server.MapPath("~/")gives null if we call it from a thread.

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

So, Try to use

所以,尝试使用

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

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

回答by user13048334

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);

回答by Guy Cothal

I know this post is a few years old, but what I do is add this line to the top of your class and you will still be able to user Server.MapPath

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

Dim Server = HttpContext.Current.Server

or u can make a function

或者你可以做一个功能

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

I am all about making things easier. I have also added it to my Utilities class just in case i run into this again.

我所做的就是让事情变得更简单。我还将它添加到我的 Utilities 类中,以防我再次遇到这个问题。