relative url in asp.net website applications

2020-02-16 04:17发布

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

3条回答
放我归山
2楼-- · 2020-02-16 04:51

You can use ~ symbol to represent root in ASP.Net

<asp:Image ID="Image1" runat="server"  ImageUrl="~/Files/pic.png"/>
查看更多
Explosion°爆炸
3楼-- · 2020-02-16 05:03

Use tilde ~ in a server control to use a relative path.

<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />
查看更多
Rolldiameter
4楼-- · 2020-02-16 05:17

@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

查看更多
登录 后发表回答