问题
I have an image that I want to use it's src attribute in relative format
when my website URL was http://localhost/
I used to use this code to access this image file:
<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
.
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
how can I change my root URL (/
) to http://localhost/mysite/
?
Thanks
回答1:
Use tilde ~
in a server control to use a relative path.
<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />
回答2:
You can use ~ symbol to represent root in ASP.Net
<asp:Image ID="Image1" runat="server" ImageUrl="~/Files/pic.png"/>
回答3:
@rrrr is right, that the way to do it,
<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />
but I would use a standard html image with runat="server"
<img runat="server" src="~/YourPath/image.png">
Reason : less server side controls
来源:https://stackoverflow.com/questions/9718730/relative-url-in-asp-net-website-applications