views:

79

answers:

5

I have an ASP .NET Website where i have an index.aspx page and 3 Folders along with it.ie;My Root has index.aspx ,FolderA,FolderB and then FolderCommon(these 3 are folders).

I have got few asp pages in FolderA and FolderB.FolderCommon stores js files ,CSS files and common code etcc....

I have a user control called pageFooter , inside which i am keeping all footer links of my page.Now my problem is when i use the same footer user control in my index page which is available in the root folder,it will not work for other pages,because the path is different.So how can i redesign the footer user control so that links will be consistent across all pages irrespective of folder structure or the place where they are being present.

Note : I dont want to give a full link to the href property (Ex: http://sitename/folderA/fielname.aspx)

Any thoughts ???

+3  A: 

If you are doing asp:Hyperlink, always set the links this way:

NavigateUrl="~/index.aspx"
NavigateURL="~/Folder/Default.aspx"

The ~ basically means "root"

If you are doing regularl <a href=""> then you need to provide the relative path to the link based off wherever the file is.

So if you are in folderA, and you want to reference the root.

<a href="../index.aspx"></a>
Jack Marchetti
I think he mentions that the same control is used in different relative paths, so you wouldn't know how relative you were to the path in your last example ("../index.aspx") as in some cases you would be at the root and other cases the same control would be used from a sub folder.
Sohnee
Well if he places the footer control in one central folder called Controls, then he will always know the path. But he should just use the "~/Folder/File.aspx" method.
Jack Marchetti
asp:Hyperlink worked .Thanks
Shyju
A: 

We stack with the same problem. We have used absolute URL with following utility to convert already existing '~' to correct path

public static class UrlUtils
{

    /// <summary>
    ///This method returns the correct relative path when installing 
    /// the portal on a root web site instead of virtual directory
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public static string GetApplicationPath(HttpRequest request)
    {
        string path = string.Empty;
        if (request.ApplicationPath != "/")
            path = request.ApplicationPath;
        return path;
    }

    /// <summary>
    /// Changes leading '~' to absolute URL including lead address e.g. http[s]://.... using
    /// <see cref="HttpContext.Current" />.
    /// </summary>
    /// <param name="url">Relative or absolute URL.</param>
    /// <returns>Absolute URL.</returns>
    public static string ResolveAbsoluteUrl(string url)
    {
        if (url.StartsWith("~"))
        {
            HttpRequest request = HttpContext.Current.Request;

            string _BaseUrl = new Uri(request.Url.ToString()).GetLeftPart(UriPartial.Authority);
            string baseUrl;
            baseUrl = _BaseUrl + GetApplicationPath(request);
            return baseUrl + url.Substring(1);
        }
        return url;
    }
Dewfy
A: 

This is (probably) what you have now...

<a href="yourpage.aspx">Link</a>

You can try:

<a href="/yourpage.aspx">Link</a>

And you should find that the slash makes all the difference!

Sohnee
A: 

I'm not sure of the exact nature of your problem, but have you tried using root-relative paths? Something like:

"~/Folder/{control or page}"

An example of your code would be helpful.

spot
A: 

Could you check the URL of the page the control is on?

Dim currentURL as string = HttpContext.Current.Request.Url.ToString

Once you know the URL you should know which links to show.

Yeodave