C# 运行时未显示图像
我正在尝试显示文件夹中的图像,该文件夹是在项目目录下按名称Images创建的 我正在尝试访问Images文件夹中的C# 运行时未显示图像,c#,asp.net,C#,Asp.net,我正在尝试显示文件夹中的图像,该文件夹是在项目目录下按名称Images创建的 我正在尝试访问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();
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);
}
TableCell tc=newtablecell();
对于(int j=0;j
我甚至在web.config
中设置了授权,但它没有用
请告诉我代码中的错误。是否尝试添加runat属性,以便从根目录解析
rating.Attributes.Add("runat", "server");
rating.ImageUrl = "~/Images/star.gif";
只需移除。此调用将返回服务器上的物理位置,该位置在客户端上不可访问
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);
}
TableCell tc=newtablecell();
对于(int j=0;j
Server.MapPath
将返回服务器上的本地路径,例如C:\Images\star.gif
。您的浏览器将无法解析该url
只需使用相对url:
rating.ImageUrl = "~/Images/star.gif";
除非另有特殊原因,否则不应该使用
Server.MapPath()
。尝试检查路径,以确保它指向正确的位置。在这种情况下,请考虑投票我的答案和/或标记为最好的答案!线等回答