asp.net: refer to a page relative to root in normal non-server-side html controls - asp.net

I've a page that is deeply nested. And one that is in the root path. Inside of the deeply nested page I have an anchor tag which is supposed to navigate to another page (which is not in the root, but it is easy to specify a root relative path).
I've done the following trying to specify a root relative path:
Home -> This one gives me a 404 error. It is not able to resolve ~ part to the root path.
The other option is to travel one directory up:
Home -> This is headache.
Then I tried this:
Home -> This gave me a 404 again. It simply removed what came after the localhost:<port_number>/ part and affixed it with /home/main.aspx.
What is the way to specify a root relative path here?
PS: I assume the root relative path will resolve for server controls

A tilde (~) is only recognized by the WebControl.ResolveUrl method, so you will have to invoke this method on the Page, which is a WebControl
<a href='<%=ResolveUrl("~/home/main.aspx") %>'>Home</a>

If you use the asp.net hyperlink control you will be able to use the '~'. If you don't want to use a servercontrol I think your stuck.
'/' will take you to the root of the site on a regular link but you have to check how you have the virtual directory is set up.

Related

redirect to a page is not working properly in asp .net which is inside sub folder

I have sub folders says X,Y,Z. I'm making menu list dynamically using 'ul', 'li' tags based on user rights. Inside 'li' tag i have 'a href="#"' tag. The tag 'a' filled from database dynamically.
The problem is the tag 'a' has link X/home.aspx. When i click first time this works since it is available. second time 404 page not found error arising. The link become X/X/home.aspx. Obviously this structure is not available. So, how to redirect.
Thanks
I don't know exactly how your href is continually being added to in this specific scenario (it's a bit of a pitfall in a number of areas, sometimes when using custom controls, etc.), however, you should qualify all of your links from the root by using a prefixed forward-slash (/, i.e. /x/home.aspx).
But we're not done, ASP.NET can transform these too, in other scenarios, so you might want to use the tilde+forward-slash prefix (~/, i.e. ~/x/home.aspx); this indicates the root of the virtual directory where the site is housed.
In this way, regardless of which level of the sites directories you are in, the links always refer to a full path, so to speak.
Your hrefs are relative from the page not from the root of your site...
So do not use "x/home" but use "/x/home".. or use "~/x/home"
Probably you are appending the directory.
If you click for 3rd time you get X/X/X/home.aspx?
Post your code in order to get more help.
Use Page.ResolveUrl
Page.ResolveUrl("~/x/home.aspx")

Problem with content page in a different folder than master page

I have put the master page in the main directory and content page in a different folder in the same directory. When I try to debug the page, it does not show me any error but the images on the master page are not visible. The content page works fine if it is in the directory itself without any folders. Can anyone let me know if I am missing something here.
You're referencing images in the master page using relative paths, relative to the location of the master page.
Since the actual page is in a different folder, those relative paths are incorrect.
Instead, you should use absolute paths, or call the ResolveUrl function to generate absolute paths from application-relative paths.

Path to css and images

For example I have site http://localhost/site
In IIS I set that 404 error causes redirection to default.aspx
If I type something like http://localhost/site/nodirectory , (there are no such folder) all works perfectly.
But if I only add slah at end http://localhost/site/nodirectory/, page can't display css and images.
Images and css are located in their own folder. I tried different paths: "gfx/logo.gif", "/gfx/logo.gif"
Does anyone have some ideas about that?
If your css and images are relative paths, say ResolveClientUrl("~/gfx/logo.gif") this renders to the client as src="gfx/logo.gif", which the browser with a slash thinks is /nodirectory/gfx/logo.gif instead of just /gfx/logo.gif.
To resolve this, don't use .ResolveClientUrl(), use .ResolveUrl(), this will make the src render src="/gfx/logo.gif" The beginning / makes it definitive, it's that path from the root of the domain.
You'll see this same hebavior if you're doing paths that start with ../ or gfx/ yourself...make them relative to the application base so there's no chance of confusion.
There are a couple of options...
1)
In your HTML page, make the path to CSS and scripts relative...
"/scripts/myscript.js"
Where the scripts folder is the first folder after the root folder
2)
You can add the base tag to your page, which means ALL page resources will be treated as relative to the root location you specify...
<base href="http://www.mysite.com">
More info about these two options.
If you can, option 1 is perhaps a bit cleaner. You know explicitly the resources that you are affecting. Using the base tag will affect ALL relative paths on your page. Images, Links, Scripts, CSS et al. The second option works best if you developed your 404 page assuming it would be in the root folder, but it could actually be referenced from any non-existent directory. You just put your root address in the base tag and it will all behave exactly as you expect.
With either option, the images can be relative to the location of your CSS file.

Visual Studio and ASP.Net Path question

Question about paths while working in Visual Studio. In my master page I have some paths to load css files as well as javascript files.
My first question is if I use relative paths, should the relative path be from the location of the master page file? For example if I keep all my master page files in a folder off the site root called MasterPages should I assume that is the starting point for my relative paths to load the css files? If that master page is used to wrap an aspx file several directories down the tree is the hard coded relative path still valid?
Second question, is there a way to use absolute paths so that everything works on my local machine as well as when I move the files up to the webroot? For example my app path on my local machine may be localhost/myappdir/default.aspx but when i move the app to the server there is no myappdir and the default.aspx is in the webroot. I do not want to have to change paths in the files after they are moved up to the server.
currently I have..
src="<%= VirtualPathUtility.ToAbsolute("~/lib/css/style.css")%>"
but this way Visual Studio cannot find the css file to update intellisence
If you use relative paths to your css/Javascript files, then they will need to be relative to the page, not the Master page.
This article does a very nice job of explaining the options you have in this situation, and towards the end they propose a nice solution to this type of problem, a user control that renders the script or link tag for you and calls ResolveClientUrl to make the paths correct.
As a rule, you should be using server-relative paths (as in, /images/stuff.gif) to everything all the time. Using relative paths (as in ../images/stuff.gif) will screw you every single time, as will trying to rely on ASP.NET "Magic" such as VirtualPathUtility.ToAbsolute and ResolveClientUrl.
Occasionally, on the server you'll need to prepend that path with a tilda (as in ~/images/stuff.gif) so that it will correctly navigate the virtual directories that ASP.NET requires you to base all your projects in.
Naturally, this also means that you'll need to set your dev box up with multisite so that you can change your root to something like http://mysite.dev/ and ensure that all your server-relative paths work correctly. (Using the VS.NET built-in server would technically work around that too, but really, do you want to use that thing???)
Use the "~" operator to resolve everything relative to the root. Never use relative paths. Even when I'm not using themes, I use their syntax to get simple syntactic access to my CSS files by creating a folder structure like : App_Themes/Style/stylesheet.css Then in my ASPX page directives I add Theme="Style" and my CSS files will be automatically resolved - not exactly conventional but very effective :-)
UPDATE:
To resolve the JS, you could use the following in your master page code-behind:
string myScript = "js/footerFix.js";
Page.ClientScript.RegisterClientScriptInclude("myKey", myScript);
With IIS7 you do not need to change anything to get "multi-site" behaviour; this is baked in. For example you can have several sites as follows and reach them via URL without any separate configuration aside from creating the virtual directories:
localhost/project1
localhost/project2
localhost/project3
Put your script and stylesheet links in a <head Id="headRegion" runat="server" /> in the master page. Their src/href should be the absolute URL path that you get when running in WebDev.WebServer (eg /ProjectWeb).
In the Master Page OnPreRenderComplete(), rewrite any URLs inside headRegion.
Visual Studio can intellisense your scripts and css, and the project can be deployed under any virtual path.
protected override void OnPreRenderComplete( EventArgs e )
{
base.OnPreRenderComplete( e );
var root = Request.ApplicationPath;
if (root == "/") {
root = "";
}
foreach (Control c in headRegion.Controls) {
if (c is HtmlLink) {
HtmlLink hlink = c as HtmlLink;
hlink.Href = ReplacePath( hlink.Href, root );
} else if (c is LiteralControl) {
var literal = c as LiteralControl;
literal.Text = ReplacePath( literal.Text, root );
}
}
}
string ReplacePath( string content, string prefix )
{
return content.Replace("/ProjectWeb", prefix );
}

ASP.NET Setting Flash file path for Object tag inside a usercontrol

I have a an ASP.NET user control where i wanto run a flash movie using flashplayer.How can i set the path of flash movie file properly so that this would work in all pages irrespective of the folders. ie; it should work inside a page in FolderA and a page in FolderASub1 which is in FolderA and a page in the Root folder too.My Flash file resides in a Folder called FlashGallery in root.My User control resides in a Subfolder in Root.
I am not sure how can use ~ here .Since its(Object tag to play flash) not a server control.
And infact i cant place the full relative path too.
Anythoughts ?
You can use a root-based path: /FlashGallery/movie.swf
Or you could generate a path string in your code, and place it in the aspx file like this:
Use an absolute path based off the root of your domain. Instead of using a relative url path like
"mymovieplayer.fla"
or
"../mymovieplayer.fla"
do this
"/flash/mymovieplayer.fla"
FYI: I use this (Free)Control: http://www.junasoftware.com/servercontrols/swfobject.net/download.aspx
You can use the '~' tilde to use relative paths to the site root and it will work on a master page, even if content pages are in different directories, you can use it like this:
<%# Register Assembly="SWFObject.Net" Namespace="Juna.Web.UI" TagPrefix="SWF" %>
.
.
.
Its FREEEEE!
Using a root based path is fine if you always know your site will be installed to the root, but this isn't realistic and best practise.
It would've been nice of we could use the server side relative path prefix of ~/ for the ... tag, but as you know it's not a user control so it just gets rendered out to the client as is. Below is a trick to allow you to specify the ~/ relative path for a client side script block.
I essentially use the VirtualPathUtility class and a protected method on the code behind of the page, master page, or control and it works very well for me.
Here's the method:
protected string GetPageRelativePath(string targetPath)
{
return VirtualPathUtility.MakeRelative( Request.AppRelativeCurrentExecutionFilePath, targetPath );
}
And here's how you can use the ~/ prefix in the script block:
<script type="text/javascript" src='<%=GetPageRelativePath("~/Scripts/MyScript.js") %>'></script>
Essentially you could use this with other src paths, even paths to images or Flash files.
Hope that helps.

Resources