HTML img 和 ASP.NET 图像和相对路径
在 ASP.NET 中引用图像以在 IIS 上进行实时部署的正确方法是什么?
以下在开发和生产中有效:
<asp:ImageButton ID="ibnEdit" runat="server" OnClick="ibnEdit_Click" ImageUrl="~/App_Themes/Default/images/one.png" Visible="false" ToolTip="Edit" />
以下在任何一个中都不起作用:(为什么不?)
<img src="~/App_Themes/Default/images/two.gif" />
以下在开发中有效,但在生产中无效:
<img src="../App_Themes/Default/images/two.gif" />
What is the correct way to reference an image in ASP.NET for live deployment on IIS?
The following works in dev and production:
<asp:ImageButton ID="ibnEdit" runat="server" OnClick="ibnEdit_Click" ImageUrl="~/App_Themes/Default/images/one.png" Visible="false" ToolTip="Edit" />
The following doesn't work in either: (why not?)
<img src="~/App_Themes/Default/images/two.gif" />
The following works in dev but not in production:
<img src="../App_Themes/Default/images/two.gif" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想使用带有
~
路径的常规img
标签,只需将runat="server"
添加到标签中作为属性即可(就像常规服务器控件一样)并且路径将被解析。例如:对于您的第二部分,../ 图像引用是否出现在多个页面上,例如用户控件或母版页(等),以便您可能在不同的文件夹级别使用它...
If you want to use a regular
img
tag with the~
path, you can just addrunat="server"
into the tag as an attribute (like regular server controls) and the path will be resolved. e.g:For your second part, is the ../ image reference appearing on more than one page, for example a user control or master page (etc) such that you might be using it at different folder levels...
我使用此语法从母版页访问图像
I use this syntax for access images from master pages
~ 仅适用于服务器控件,例如
或
。这告诉 ASP.Net 插入应用程序路径。有时这只是"/"
但如果您的应用程序不是网站的根目录,它将包含它所在的路径。img
标记只是 html,它会不会被 ASP.Net 更改,因此浏览器获取路径"~/App_Themes/Default/images/two.gif"
并且不知道如何读取它。我不知道为什么最后一个示例在开发中有效但在生产中无效。这可能与应用程序位于 dev 的根目录中但位于生产中的子目录有关。
The ~ will only work on a server control such as
<asp:Image>
or<asp:ImageButton>
. This tells ASP.Net to insert the application path. Sometime that's just"/"
but if your application is not the root directory of the website it will include the path it is in. Theimg
tag is just html and it will not be altered by ASP.Net, so the browser gets the path"~/App_Themes/Default/images/two.gif"
and doesn't know how to read it.I don't know why the last example works in dev but not in production. It might has something to do with having the application in the root directory in dev but in a sub directory in production.
这对我有用
$(".selector").attr('src', "Content/themes/base/images/img.png");
重要的是你的新 src 的开头没有“/”。
This worked for me
$(".selector").attr('src', "Content/themes/base/images/img.png");
The important is that you do not have "/" at the beginning of your new src.
你可以看看我的回答。我已经使用C#语言解决了这个问题。
为什么我不能做 ?
You can look into my answer . I have resolved the issue using C# language.
Why can't I do <img src="C:/localfile.jpg">?