Newbie question...
If I have a file that is in the root of the web app. How do I programmaticaly query the path of that file? ie, what directory it is in?
如果我有一个位于Web应用程序根目录中的文件。我如何以programmaticaly方式查询该文件的路径?即,它在哪个目录?
4 个解决方案
#1
2
System.Web.HttpServerUtility.MapPath( "~/filename.ext" );
will give you the physical (disk) path, which you would use with System.IO methods and such.
将为您提供物理(磁盘)路径,您将使用System.IO方法等。
System.Web.Hosting.VirtualPathUtility.ToAbsolute( "~/filename.ext" );
will give you the "absolute" virtual path. This won't be the full url, but isn't necessarily the root of the domain, either. It could be something like
会给你“绝对”的虚拟路径。这不是完整的URL,但也不一定是域的根。它可能是这样的
/admin/filename.ext
if the application is rooted in a subdirectory.
如果应用程序植根于子目录中。
#2
0
was close to what I was wanting..... except that didn't seem to compile or wasn't valid in the context I was calling it.
接近我想要的.....除了似乎没有编译或在我调用它的上下文中无效。
However I found what I needed with System.Web.HttpRuntime.AppDomainAppPath
但是我通过System.Web.HttpRuntime.AppDomainAppPath找到了我需要的东西
#3
0
If you are in your ASPX markup you can break out to C# and use the ResolveUrl method like so:
如果你在ASPX标记中,你可以突破到C#并使用ResolveUrl方法,如下所示:
<%= Page.ResolveUrl("~/PathFromRoot/YourFile.pdf") %>
#4
0
Old question, I know, but I found it while searching for a similar answer. Unless the API has changed, the reason why harpo's answer isn't working is because MapPath
is an instance method, not a static method. But fear not--there's an instance of HttpServerUtility
present in each instance of Controller
--the Server
property. So in your case, if you're within a controller (or, I suspect, a view):
老问题,我知道,但我在寻找类似的答案时找到了它。除非API已经改变,否则harpo的答案不起作用的原因是因为MapPath是一个实例方法,而不是静态方法。但不要害怕 - 在Controller的每个实例中都存在HttpServerUtility的实例--Server属性。所以在你的情况下,如果你在一个控制器内(或者,我怀疑,一个视图):
var appRoot = Server.MapPath("~/");
That should do the trick!
这应该够了吧!
#1
2
System.Web.HttpServerUtility.MapPath( "~/filename.ext" );
will give you the physical (disk) path, which you would use with System.IO methods and such.
将为您提供物理(磁盘)路径,您将使用System.IO方法等。
System.Web.Hosting.VirtualPathUtility.ToAbsolute( "~/filename.ext" );
will give you the "absolute" virtual path. This won't be the full url, but isn't necessarily the root of the domain, either. It could be something like
会给你“绝对”的虚拟路径。这不是完整的URL,但也不一定是域的根。它可能是这样的
/admin/filename.ext
if the application is rooted in a subdirectory.
如果应用程序植根于子目录中。
#2
0
was close to what I was wanting..... except that didn't seem to compile or wasn't valid in the context I was calling it.
接近我想要的.....除了似乎没有编译或在我调用它的上下文中无效。
However I found what I needed with System.Web.HttpRuntime.AppDomainAppPath
但是我通过System.Web.HttpRuntime.AppDomainAppPath找到了我需要的东西
#3
0
If you are in your ASPX markup you can break out to C# and use the ResolveUrl method like so:
如果你在ASPX标记中,你可以突破到C#并使用ResolveUrl方法,如下所示:
<%= Page.ResolveUrl("~/PathFromRoot/YourFile.pdf") %>
#4
0
Old question, I know, but I found it while searching for a similar answer. Unless the API has changed, the reason why harpo's answer isn't working is because MapPath
is an instance method, not a static method. But fear not--there's an instance of HttpServerUtility
present in each instance of Controller
--the Server
property. So in your case, if you're within a controller (or, I suspect, a view):
老问题,我知道,但我在寻找类似的答案时找到了它。除非API已经改变,否则harpo的答案不起作用的原因是因为MapPath是一个实例方法,而不是静态方法。但不要害怕 - 在Controller的每个实例中都存在HttpServerUtility的实例--Server属性。所以在你的情况下,如果你在一个控制器内(或者,我怀疑,一个视图):
var appRoot = Server.MapPath("~/");
That should do the trick!
这应该够了吧!