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.
Related
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") %>' />
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.
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
I have a master page with links to other pages in the site. Those links use tilde paths (like "~/dir1/page2.aspx"). On most of the pages in the site that use this master page, there is no problem.
The problem only occurs on a few pages that use the master page. The links are VERY wrong; it tries to use the ~ as part of the link (so they are "http://server.domain.com/~/dir1/page2.aspx").
It's as if it is treating the tilde as a literal under certain circumstances.
Sounds like you're not properly resolving the URLs.
Are you writing ResolveUrl("~/")?
Also make sure that if you use ~/ that your controls are runat="server".
I just had this issue and the answer that worked best for me was to use the asp:Hyperlink control:
<asp:HyperLink ImageUrl="/Images/Logo.PNG" runat=server NavigateUrl="~/Default.aspx" />
I've got a couple of ASP.Net Usercontrols that I am using in different locations of my new website. These usercontrols had links like this :
If the usercontrol is used in pages in various subdirectories the relative path just doesn't work, and I don't want to provide my full website name in the path. So I did this
<a href="~/daily/panchang/" runat="server">
and now the ASP.Net '~' marker works correctly to resolve the root path.
Is it okay to mark all my HTML tags where I have the need to resolve the root path with runat="server" or do you know of a better, HTML way?
Thanks
I won't say whether it's an elegant solution, I'll just point out an alterantive within System.Web:
<a href="<%= VirtualPathUtility.ToAbsolute("~/daily/panchang/") %>">
You should use a base tag to define the root of your application and make all links relative like this :
<head>
<base href="<%= Request.ApplicationPath %>" />
</head>
...
<!-- this now points to ~/daily/panchang/ -->
Be careful though because every element that has runat="server" will be 'serialized' and stored in the ViewState every time a PostBack occurs, and you don't wanna be cluttering it up with useless data.