skip to Main Content

I have a MasterPage that is applied to 2 subdirectories like so:

Root
|    MasterPage.Master
|
└────Options
|    |    default.aspx
|    |    config.aspx
|
└────Settings
     |    default.aspx
     |    config.aspx

I have a navigation menu in the MasterPage.Master and I want the hyperlinks there to go to the file name of the local directory, some example links would be:

<asp:HyperLink runat="server" NavigateUrl="config.aspx">Go To Config</asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl="default.aspx">Do Something Else</asp:HyperLink>

For example, if I’m currently at the URL mysite.com/Options and I click a hyperlink to go to config.aspx, I want to navigate to mysite.com/Options/config.aspx.

Setting the hyperlink’s NavigateUrl property to ./config.aspx or config.aspx both result in being linked to mysite.com/config.aspx since the MasterPage.Master lives in the root directory.

How can I set the hyperlink to go to the config.aspx option local to the current subdirectory I’m in? Is it possible just from setting the NavigateUrl without doing anything from the code behind?

2

Answers


  1. Chosen as BEST ANSWER

    I ended up creating a function to handle this as I couldn't find anything that would allow that level of relative control on the .aspx page. It's not pretty, but all the directories inside where this is being used will only be for site navigation:

    private void GenerateLinks()
    {
        Panel leftNav = pnlLeftNav;
    
        foreach (var dir in Directory.GetDirectories(Server.MapPath(".")))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
    
            HyperLink link = new HyperLink();
            link.Text = dirInfo.Name;
            link.NavigateUrl = dirInfo.Parent.Name + "/" + dirInfo.Name;
    
            leftNav.Controls.Add(link);
        }
    }
    

    I call this in my PageLoad() on the Master Page.


  2. Define the paths for the master page to ALWAYS include full path.

    While it ok in a child page, or regular page inside of a folder to have links and navagation JUST based on the aspx name, for the master page, since we could be ANYWHERE in the folders, then all links in that master page need and should be FULL path names – from the root of the application.

    eg like this:

    href="~/SiteAdmin/EditChoices"
    

    or to the root simple default.aspx page, then this:

     href="~/Contact.aspx"
    

    So, "~/" means from base root of the site. And for the master page, then you quite much will NOT have any type of relative addressing – as it will not make sense, since you could be several folders deep into some particular section of the web site, but the menu bar is still in full view from the master page, and thus for any menu bar in master page to work, it can’t use relative addressing, but ALWAYS use full path names.

    So, depending on which config page you want, then

    NavigateUrl="~/Settings/config.aspx"
    

    or

    NavigateUrl="~/Options/config.aspx"
    

    Now, if say some sub menu, or menu is built on the child page (not master), then of course you can use and assume current folder – but master page is in view at all times – so it needs full path names to work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search