简体   繁体   English

访问根目录中的文件夹

[英]Access folders in root directory

Hi I am developing an asp.net web application. 嗨,我正在开发一个asp.net Web应用程序。 I have to access one of the image in images folder in root directory. 我必须访问根目录下images文件夹中的图像之一。 I am using following code in my code behind file. 我在文件后面的代码中使用以下代码。

string imageDirectory = HttpContext.Current.Server.MapPath("~/images/");
string imageUrl = imageDirectory + "/img1.bmp";

This works fine in my local machine. 这在我的本地计算机上工作正常。 My question is does this code work when I move my application to production ? 我的问题是,当我将应用程序移至生产环境时,此代码是否有效?

If you're thinking of putting imageUrl into an <img> tag, then no, it won't work. 如果您打算将imageUrl放入<img>标记中,那么不会,它将无法正常工作。 Server.MapPath will return your file or directory as a local Windows file/directory name, so something like "C:\\WebRoot\\MyWebApplication". Server.MapPath将以本地Windows文件/目录名称返回文件或目录,因此类似“ C:\\ WebRoot \\ MyWebApplication”。 If you send this to the browser, obviously, the browser won't pick up the image. 如果将其发送给浏览器,很明显,浏览器将不会拾取图像。

What you can do is something like: 您可以做的是:

string imageUrl = ResolveClientUrl("~/images/myImage.gif");

It should as long as you have an application root/virtual directory for your site. 只要您拥有站点的应用程序根目录/虚拟目录即可。

Also, you can combine these two lines into: 另外,您可以将这两行合并为:

string imageUrl = HttpContext.Current.Server.MapPath("~/images/img1.bmp");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM