[英]Image not showing up in runtime
我正在尝试显示在项目目录下通过名称“图像”创建的文件夹中的图像。
我正在尝试访问Images文件夹中的star.gif
。
这是我访问图像的一小段代码
TableCell tc=new TableCell();
for (int j = 0; j < i; j++)
{
System.Web.UI.WebControls.Image rating = new System.Web.UI.WebControls.Image();
rating.ImageUrl = Server.MapPath("~/Images/star.gif");
rating.ID = j.ToString();
rating.AlternateText = "No image";
tc.Controls.Add(rating);
}
我什至在web.config
设置了授权,但没有用。
请在代码中告诉我错误。
只需删除Server.MapPath即可 。 该调用将返回服务器上的物理位置 , 客户端无法访问该位置 !
MSDN Server.MapPath
MapPath方法将指定的相对或虚拟路径映射到服务器上的相应物理目录。
新密码
TableCell tc=new TableCell();
for (int j = 0; j < i; j++)
{
System.Web.UI.WebControls.Image rating = new System.Web.UI.WebControls.Image();
rating.ImageUrl = "~/Images/star.gif"; // no need for Server.MapPath
rating.ID = j.ToString();
rating.AlternateText = "No image";
tc.Controls.Add(rating);
}
Server.MapPath
将返回服务器上的本地路径,例如C:\\Images\\star.gif
。 您的浏览器将无法解析该网址。
只需使用相对网址:
rating.ImageUrl = "~/Images/star.gif";
尝试添加runat属性,以便从根目录解析?
rating.Attributes.Add("runat", "server");
rating.ImageUrl = "~/Images/star.gif";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.