I'm confused with ASP.NET relative path, please can someone help?
In a Master Page I gave a link label referencing:
Login
From the ASP.NET official documentation I read:
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" />
With the Login markup, when I click the link from a page in the /Account folder, I'm redirected to:
/Account/~/Account/Login.aspx
Why? WHY?h
Because you're using it directly in markup, rather than in a server control. Something as simple as this should fix it:
<a runat="server" href="~/Account/Login.aspx">Login</a>
Basically, the ~ path reference needs to be translated on the server, since it's a reference to the server path of the application's base directory. Plain HTML markup isn't processed on the server, it's just delivered as-is to the client. Only server-processed code will translate the ~ path to what it resolves to.
use this command
<a href="<%=Page.ResolveUrl("~/product.aspx")%>" >Link To Products</a>
You can use ~ when refering to URLs inside ASP.NET Server Controls.
You are using it in a <a> tag which is just plain html that doeesn't know anything about ~ . use '"/Images/SampleImage.jpg"' instead
Related
For example, I have two sibling pages Index.aspx and Orders.aspx in one folder. On the page Index.aspx I have the link to Orders.aspx. What is the correct way to implement this:
<a runat="server" href="~/Orders.aspx">
or
<a href="Orders.aspx">
I know what runat="server" does (server-control, performance impact etc.).
You really never need to run markup with a run at server tag if it's never used in code behind, if it is then you should use a ASP.NET control for it.
So just a hyperlink without runat=server would be fine.
It's always best to use ASP.NET controls on your page though if an upgrade in the future could require language translations, or have some logic assigned to them in the future. So always plan ahead on your designs.
If both views are in the same folder, than the second one:
<a href="Orders.aspx">
Suppose this scenario. On my Default.aspx, I insert a context (WebForms), called MyContext.ascx, that load an image. So the path would be :
<img src="/images/hello.gif" />
Well. Now, I'd like to insert the same context on another .aspx page, that is in another forlder, such as /myfolder/MyPage.aspx
Than, the path of the image now should be :
<img src="../images/hello.gif" />
How you can see, I can't manage two different path for the same context. So, is there a way (symbol) to call the virtual-path of my application? Without using my own function as
<img src="<%=MyUtilities.GiveVirtualPath%>/images/hello.gif" />
which is boring. Who know?
Put your images in the root of your web-site (and inside a resources/styles/themes folder).
You can use the tilde ~ to indicate the root of your site. All your pages will refer to that. If you're using server side controls you do not even need to use the ResolvePath() method (in your example you should use it, if for example you wrote asp:image ImageUrl="" you do not need to. From MSDN.
I use a ListView to display (funnily enough) a list of data, including a hyperlink. Here is my item template (ascx file):
<a href='<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "ID","/Pages/Image.aspx?id={0}").ToString()%>'
title='View <%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "Title")%>'>
<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "CardNo")%> -
<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "Title")%></a>
I'm having problems with the link resolving correctly as this same template is re-used multiple times across the project and fails for pages in a subfolder. I guess I'm looking for a Server.MapPath equivalent so that I could use ~ to get a path relative to root but how do I insert that into my HTML?
Update: The problem is caused by the fact that my project runs in a subfolder of LocalHost root on my machine. In Production the project is in the root itself and this problem doesn't occur.
You're looking for ResolveClientUrl or ResolveUrl.
What is the correct way to reference an image in ASP.NET for live deployment on IIS?
The following works in dev and production:
<asp:ImageButton ID="ibnEdit" runat="server" OnClick="ibnEdit_Click" ImageUrl="~/App_Themes/Default/images/one.png" Visible="false" ToolTip="Edit" />
The following doesn't work in either: (why not?)
<img src="~/App_Themes/Default/images/two.gif" />
The following works in dev but not in production:
<img src="../App_Themes/Default/images/two.gif" />
If you want to use a regular img tag with the ~ path, you can just add runat="server" into the tag as an attribute (like regular server controls) and the path will be resolved. e.g:
<img src="~/App_Themes/Default/images/two.gif" runat="server" />
For your second part, is the ../ image reference appearing on more than one page, for example a user control or master page (etc) such that you might be using it at different folder levels...
I use this syntax for access images from master pages
<img src="<%=ResolveUrl("~/Content/Images/error_img.jp")%>" width="350" style="padding-right: 15px; padding-top: 20px;"/>
The ~ will only work on a server control such as <asp:Image> or <asp:ImageButton>. This tells ASP.Net to insert the application path. Sometime that's just "/" but if your application is not the root directory of the website it will include the path it is in. The img tag is just html and it will not be altered by ASP.Net, so the browser gets the path "~/App_Themes/Default/images/two.gif" and doesn't know how to read it.
I don't know why the last example works in dev but not in production. It might has something to do with having the application in the root directory in dev but in a sub directory in production.
This worked for me
$(".selector").attr('src', "Content/themes/base/images/img.png");
The important is that you do not have "/" at the beginning of your new src.
byte[] imageArray = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/Upload_Image.png"));
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
You can look into my answer . I have resolved the issue using C# language.
Why can't I do <img src="C:/localfile.jpg">?
My application paths …
<a runat="server" href="~/Home">
…are resolving to “Home” (not “/Home”). After I rewrite URLs, “/Blah/Blah”, all the ”~/” links are relative to the rewrite: /Blah/Home
Is there a way to force a root to “/”?
Why don't you just write the links relative to the root ('/') instead of '~/', if you're application is not at the root of the domain, then the '~/' links will resolve to the root of the application
If you're sure that, for the links in question you'll always be at a root of "/", then the simplest thing to do is change the <a> so that the href reads "/Home" rather than "~/Home" ... That way asp.net won't parse it and change it to use the App/VDir as its starting point.
If you use standard HTML tags like a, then include the url via
...
or use asp.net hyperlinks:
<asp:Hyperlink NavigateUrl="~/Home" runat="server" ID="HomeLink" Text="..." />
That way, all links will point to the right URL, even when the web application will be installed in a subdirectory.
<% %> is inline coding for asp.net, and <%= %> outputs the content, in that case the result of ResolveUrl.
~/ gets translated in the web controls, as in not Otherwise, you need to use ResolveClientUrl to do that.
For the hyperlink control, it will automatically map correctly for you.