一旦我点击按钮,我的image1就会显示一个图像;
当我将imagePath设置为
Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
我的url不起作用
但是当我将我的路径设置为
"~/SME IMAGE/anonymous_avatar.gif"
我的网址起作用了
那么基本上我的Server.MapPath会发生什么呢?
try
{
string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
Image1.ImageUrl = imagePath;
Response.Write("success");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
发布于 2012-02-03 08:08:34
Server.MapPath
它将"web“路径转换为您的本地路径。
所以Server.MapPath("Server.MapPath")
会给出你的文件的本地位置。
您将其赋值给ImageUrl
属性,但它将被转换为类似于"www.mywebsite.com/C:\wwwroot\somefile.gif"
的内容
发布于 2012-02-03 08:06:02
尽量不要设置Server.MapPath
-只需使用:
Image1.ImageUrl = "~/SME IMAGE/anonymous_avatar.gif";
也许可以确保url存在,或者添加%20
而不是路径中的空格。(还要尽量避免路径中的空格)
发布于 2012-02-03 08:09:38
Server.MapPath
返回与网络服务器上指定的虚拟路径相对应的物理文件路径。因此,当您设置
string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
它可能会返回类似C:\Inetpub\wwwroot\website\SME IMAGE\anonymous_avatar.gif
的内容,这实际上是文件的物理路径。
Image.ImageUrl
属性接受要在Image
控件中显示的图像的URL。您可以使用相对URL或绝对URL。MSDN说:
相对
将图像的位置与网页的位置相关联,而不指定服务器上的完整路径。该路径是相对于网页的位置的。这使得在不更新代码的情况下将整个站点移动到服务器上的另一个目录变得更容易。绝对URL提供了完整的路径,因此将站点移动到另一个目录需要更新代码。
https://stackoverflow.com/questions/9125389
复制相似问题