在ASP.NET外部C#类中使用使用Server.Mappath [英] Using Server.MapPath in external C# Classes in ASP.NET
问题描述
我想获得一个C#类的某些文件的绝对路径。 使用Server.Mappath
为ASPX的伟大工程,当然和他们的code隐藏页,但没有在另一个类文件存在。我试图 HostingEnvironment.MapPath()
,但抱怨说,相对虚拟路径是不允许的。有什么想法?
I'm trying to get the absolute path of certain files in a C# class. Server.MapPath
works great of course for ASPX and their code-behind pages, but that doesn't exist in another class file. I tried HostingEnvironment.MapPath()
, but that complains that the relative virtual path isn't allowed. Any thoughts?
的System.Web
已被导入。
推荐答案
的 ServerUtility
类可作为一个实例你的的HttpContext
。如果你在你知道它会在ASP.Net管道内执行的环境的时候,你可以用
The ServerUtility
class is available as an instance in your HttpContext
. If you're in an environment where you know it'll be executed inside the ASP.Net pipeline, you can use
HttpContext.Current.Server.MapPath()
您必须导入的System.Web
虽然。
这篇关于在ASP.NET外部C#类中使用使用Server.Mappath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!