服务器.MapPath()
我想使用 Server.MapPath() 方法来映射虚拟目录 我创建了它的物理路径。
问题是 .net 环境无法识别 Server.MapPath()。
Google 告诉我我应该使用 System.Web 来使用 HttpContext.Current.Server, 但尽管我使用 System.Web,但 HttpContext 未被识别。 (我已经检查过 - HttpContext 是 System.Web 的类之一)
有帮助吗?
I want to use Server.MapPath() method im order to map a virtual directory
I created to its physical path.
The thing is that the .net environment doesn't recognize Server.MapPath().
Google told me I'm supposed to use HttpContext.Current.Server using System.Web,
but HttpContext isn't recognized in spite of me using System.Web. (And I've checked -
HttpContext IS one of System.Web's classes)
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的项目中已包含 System.Web
执行这些操作(在 Visual Studio IDE 中):
Server.MapPath 现在应该可用。
Make sure you have included System.Web in your projects References
Do these (In Visual Studio IDE):
Server.MapPath should now be available.
如果您有一个 Web 应用程序,您应该自动拥有对 System.Web.dll 的引用,并且您应该有权访问 System.Web.HttpContext 类。检查您是否意外删除了引用。您需要使用
using System.Web;
语句来访问HttpContext
类,而不指定完整的命名空间。如果您没有 Web 应用程序,则必须添加对 System.Web.dll 的引用才能访问 HttpContext 类,但这对您没有帮助一点。由于您不在 Web 应用程序中,因此没有 HTTP 上下文,也没有 Web 根文件夹,因此您无法使用 MapPath 方法。
If you have a web application, you should automatically have a reference to
System.Web.dll
, and you should have access to theSystem.Web.HttpContext
class. Check that you haven't accidentally removed the reference. You would need ausing System.Web;
statement to access theHttpContext
class without specifying the complete namespace.If you don't have a web application you would have to add a referece to
System.Web.dll
to get access to theHttpContext
class, but that would not help you a bit. As you are not in a web application, there is no HTTP context and there is no web root folder, so you can not use the MapPath method.这里同样的问题。在 ASP.net 4.0 Web 应用程序的 .ashx 处理程序中,顶部有
using System.Web
。我无法使用Server.MapPath()
(这是我在书中所说的)或System.Web.HttpServerUtility.MapPath()
(这是 Google 和 MSDN 的内容)继续出现。我也无法使用上面提到的 HttpServerUtility.MapPath() 。但是,这里的其他答案之一提示我尝试
context.Server.MapPath()
,它在我的ProcessRequest(HttpContext context)
方法中起作用。Same problem here. In an ASP.net 4.0 web application, in a .ashx handler, with a
using System.Web
at the top. I couldn't useServer.MapPath()
which is what the book I have says to use orSystem.Web.HttpServerUtility.MapPath()
which is what Google and MSDN keep turning up. I also couldn't useHttpServerUtility.MapPath()
as mentioned above.However, one of the other answers here prompted me to try
context.Server.MapPath()
which does work in myProcessRequest(HttpContext context)
method.