[英]Cannot use Server.MapPath
What I must do to make Server.MapPath
work?我必须做什么才能使
Server.MapPath
工作?
I have using System.Web;
我有
using System.Web;
what else?还有什么? When I type
Server
there is no quick result option (intelli-sense) for Server
.当我键入
Server
不存在用于快速结果选项(智能影音感) Server
。
Any help?有什么帮助吗?
you can try using this你可以尝试使用这个
System.Web.HttpContext.Current.Server.MapPath(path);
or use HostingEnvironment.MapPath
或使用
HostingEnvironment.MapPath
System.Web.Hosting.HostingEnvironment.MapPath(path);
Your project needs to reference assembly System.Web.dll
.您的项目需要引用程序集
System.Web.dll
。 Server is an object of type HttpServerUtility
. Server 是
HttpServerUtility
类型的对象。 Example:例子:
HttpContext.Current.Server.MapPath(path);
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("~/")
Firt add a reference to System.web
, if you don't have.如果没有,请先添加对
System.web
的引用。 Do that in the References folder.在References文件夹中执行此操作。
You can then use Hosting.HostingEnvironment.MapPath(path);
然后你可以使用
Hosting.HostingEnvironment.MapPath(path);
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);
尝试添加System.Web
作为对您的项目的引用。
您需要添加对System.Web
引用( System.Web
)引用
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 类中,以防我再次遇到这个问题。
I faced the same problem and I guess this might help someone.我遇到了同样的问题,我想这可能会对某人有所帮助。 As the Original Poster of this question asked
正如这个问题的原始海报所问的
What I must do to make Server.MapPath work?
我必须做什么才能使 Server.MapPath 工作?
I have using System.Web;我有使用 System.Web;
The class which we had written should implement System.Web.UI.Page我们编写的类应该实现System.Web.UI.Page
Say for example, our classname is MyClass比如说,我们的类名是MyClass
public class MyClass: System.Web.UI.Page
{
// Code runs here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.