relative url in asp.net website applications

强颜欢笑 提交于 2019-12-12 03:19:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!