简体   繁体   English

asp.net网站应用程序中的相对URL

[英]relative url in asp.net website applications

I have an image that I want to use it's src attribute in relative format 我有一张要以相对格式使用它的src属性的图像

when my website URL was http://localhost/ I used to use this code to access this image file: 当我的网站URL是http://localhost/我曾经使用此代码访问此图像文件:

<img alt="something" src="/Files/pic.png">

But now I have to add an application to my site and change my site URL to http://localhost/mysite . 但是现在我必须将应用程序添加到我的站点,并将站点URL更改为http://localhost/mysite

Now none of my images load in this site because the path is still http://localhost/Files/pic.png not http://localhost/mysite/Files/pic.png 现在我的图像都没有加载到该站点中,因为路径仍然是http://localhost/Files/pic.png而不是http://localhost/mysite/Files/pic.png

how can I change my root URL ( / ) to http://localhost/mysite/ ? 如何将我的根URL( / )更改为http://localhost/mysite/

Thanks 谢谢

在服务器控件中使用代字号~以使用相对路径。

<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />

您可以使用〜符号表示ASP.Net中的根

<asp:Image ID="Image1" runat="server"  ImageUrl="~/Files/pic.png"/>

@rrrr is right, that the way to do it, @rrrr是正确的,这样做的方式,

<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" /> 

but I would use a standard html image with runat="server" 但我会使用带有runat =“ server”的标准html图像

<img runat="server" src="~/YourPath/image.png">

Reason : less server side controls 原因:较少的服务器端控件

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

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