relative url in asp.net website applications - asp.net

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

Use tilde ~ in a server control to use a relative path.
<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />

You can use ~ symbol to represent root in ASP.Net
<asp:Image ID="Image1" runat="server" ImageUrl="~/Files/pic.png"/>

#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

Related

ASP.NET redirect pagename.aspx/something to pagename.aspx

Ok, I didn't know how to phrase this differently. Basically, I have an issue on my website, where writing pagename.aspx/anything will open the page pagename.aspx but with no styles or images cause they have relative paths. It's not possible to rewrite them to have absolute paths. Is there any way I could make it work somehow (like for example, redirecting it)? Thanks in advance.
For converting any application root relative URL (which is the most common format) to the appropriate URL on the client in a Web Forms page, you can use this code:
<%# ResolveClientUrl("~/path/to/file.css") %>
Example:
<script src='<%# ResolveClientUrl("~/Scripts/jquery-2.1.0.min.js") %>' type="text/javascript"></script>
However, in cases where you're using server side controls (for example, asp:Image instead of plain img element) then you can use the application root relatively URL without needing to resolve it.
<asp:Image runat="server" ImageUrl="~/images/myimage.jpg" />
is equivalent to
<img src='<%# ResolveClientUrl("~/images/myimage.jpg") %>' />

How to pass image url from vb.net to asp.net?

I m fetching the image name path from vb.net and want to pass it to image URL in asp.net.. How to do... I m doing this but display nothing
IN Vb.net
dim myLogo as string = ResolveUrl("C:\Test\Logo\" & img_name)
Me.DataBind()
IN ASP.net
<asp:Image ID="test" ImageUrl='<% myLogo %>' runat="server" Height="100px" Width="100px" />
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
<asp:image runat="server" id="Image1"
ImageUrl="~/Images/SampleImage.jpg" />
You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.
For more details refer:
ASP.NET Web Project Paths
Eg.
dim myLogo as string = "~\Logo\" & img_name
Surely the url to the file will be:
"file://c:\Test\Logo\" & img_name
Have you tried that?
Try this
<asp:Image ID="test" ImageUrl='<%= myLogo %>' runat="server" Height="100px" Width="100px" />
You need to declare myLogo variable as protected in general section of code and in aspx page you can use folling code to bind imageurl.
<asp:image runat="server" Height="100px" Width="100px" imageurl='<%#myLogo%>' />
Please let me know if this does not work.

Image of asp:ImageButton getting displayed using relative path, but not with absolute path

<asp:ImageButton runat="server" ID="cal_btn1"
AlternateText="image" ImageUrl="~/Images/calendar_icon1.png"/>
The image gets displayed with the above code, but I have no idea what happens when I use the absolute path-
<asp:ImageButton runat="server" ID="cal_btn1"
AlternateText="image" ImageUrl="C:\Users\adwivedi\Documents\Visual Studio 2010\WebSites\WebSite1\Images\calendar_icon1.png"/>
I replaced '\' with '\\', but still no change. Any idea what's wrong? Thanks!
It's not working because you have to use an url, not a physical path.
Use the ImageUrl property to specify the URL of an image to display in the Image control. You can use a relative or an absolute URL. A relative URL relates the location of the image to the location of the Web page without specifying a complete path on the server. The path is relative to the location of the Web page. This makes it easier to move the entire site to another directory on the server without updating the code. An absolute URL provides the complete path, so moving the site to another directory requires that you update the code.
Absolute Url
<asp:ImageButton runat="server" ID="cal_btn1"
AlternateText="image" ImageUrl="http://mydomain/Images/calendar_icon1.png"/>
Relative Url
<asp:ImageButton runat="server" ID="cal_btn1"
AlternateText="image" ImageUrl="Images/calendar_icon1.png"/>
Application Root Relative Url
<asp:ImageButton runat="server" ID="cal_btn1"
AlternateText="image" ImageUrl="~/Images/calendar_icon1.png"/>
I suggest you to take a look to this MSDN article in relation to asp.net paths.

Logo not appearing on some pages in ASP.net webforms title

ill try my best to explain this..
in the site.master I have added a logo to the title like so.
<div class="title">
<img alt="" class="style1" src="Images/logo.png" />
</div>
the logo shows up fine in all pages accept when the url is part of the account.
for example..
"http://localhost:54341/Sitename/mypage.aspx" (shows fine)
however
"http://localhost:54341/Sitename/Account/Register.aspx" (doesnt show)
im guessing its because it is in a parent folder (the images file).
Is there a better way?
EDIT: Images folder is at the top level
thanks in advance
Try using relative path with respect to root of the application:
<img alt="" class="style1" runat="server" src="~/Images/logo.png" />
Explaination:
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
<asp:image runat="server" id="Image1"
ImageUrl="~/Images/SampleImage.jpg" />
You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.
This should do it...
<img alt="" class="style1" src="<%=ResolveClientUrl("~/Images/logo.png")%>" />
#Kevin Main suggested : You need to put runat="server" on the image for this to work.. this worked a treat
This will work for localhost:54341/Sitename/Account/Register.aspx
<img alt="" class="style1" src="../Images/logo.png" />

How to give path for image in a master page for two sub-pages in two locations?

Hi all my work environment is asp.net c# vs2008. My issue is this, i have a master page outside.master in shared folder.Inside it i have an image control with
<img src="App_Themes/Home/images/logo.png" />
i am referring this master page from in two sub pages. One is Index.aspx which is located in the root level and Secondly registration.aspx which is under masters folder. The problem is that when i run, the index.aspx will show the logo where registration.aspx is not showing the logo. Please tell me how to specify the path so that i will get logo in both pages.
Tilde sign ~ will resolve for server side controls.
So you need to add runat="server" as img in HTML element.
Try this:
<img src="~/App_Themes/Home/images/logo.png" runat="server"/>
The most foolproof method is to have something like this
<asp:Image runat="server" id="myImage" ImageUrl='<%# Eval("imageFile") %>' />
Then in the code-behind assign the variable imageFile to something like Server.MapPath("App_Themes/Home/images/logo.png");
Try the following:
<img src="~/App_Themes/Home/images/logo.png" runat="server" />

Resources