I've been stuck with this annoying little problem for a few days now. I'm building a new face for my company's website, and I can't post the original code due to legal reasons, but I'll try to explain and add a code with different names so you guys can understand better.
I have an aspx page called Index.aspx, which stays inside a folder called Home. Folder Home stays inside folder Views.
On the Index.aspx, I put my content, which is the introductory text and a bootstrap carousel, inside these tags:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"\>
Index.aspx points to SystemsMenu.Master, which is where the navbar and the footer stay so we can avoid repetition of code and links. Inside the Home folder, there’s another folder that has aspx pages for a dropdown menu on the navbar, let’s call this dropdown menu MEFS.
The aspx pages for the MEFS links are the same structure as the Index.aspx page, the content (introductory texts) stays inside the asp:content tag, and all of the MEFS aspx pages point to SystemsMenu.Master, which is where the navbar with all the link stays.
So, on SystemsMenu.Master, the code for the MEFS menu goes like this:
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="MEFSDropdown" role="button" data-bs-toggle="dropdown" data-bs-auto-close="outside">MEFS</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a href="MEFS/AboutMEFS.aspx">About></li></ul></li>
<li><a href="MEFS/Documentation.aspx">Documentation</a></li>
<li><a href="MEFS/MEFSAccess.aspx">Access to MEFS</a></li>
<li><a href="MEFS/FAQMEFS.aspx">FAQ – Frequently asked questions</a></li>
</ul>
</li>
When the page loads and I click to open the MEFS dropdown menu, and then click on the Documentation link, for an example, it loads the page normally. However, if I click to open the MEFS dropdown menu again (still on the Documentation page!), and I click on another link, the url path starts repeating itself, giving me an error afterwards.
Example: First click, no problem: /Views/Home/MEFS/Documentation Second click, problem: /Views/Home/MEFS/MEFS/FAQMEFS
Error: Server Error in '/' Application.
The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Views/Home/MEFS/MEFS/FAQMEFS.aspx
(I'm sorry if this question seems obvious or dumb to you, but I'm fairly new to coding and I'd really like to learn...)
I have tried changing it to:
/MEFS/FAQMEFS ../MEFS/FAQMEFS ../../FAQMEFS ../Home/MEFS/FAQMEFS
None of these work.... (I have tried looking for similar questions here but the only one who is EXACTLY like mine doesn't have any answers :( ).
I would suggest that you "force" the links to be evaluated server side.
While in a regular href, or even any client side script you can't use "~/" for the root.
However, if you add a runat="server", then you can force the path to be from root to current. In most cases, you don't need to do this, but since the master page is in fact launched/used/consumed by a child .aspx page, then the relative addresing can get messued up.
So, use this format:
Now, in above, I also added a "id", since I OFTEN have code that will hide menu bar options based on user's role membership.
Say like this in page load on master page:
So, not only does adding a "id" tag, and runat="server" mean You can use a full path name (from root), but it also means code behind with great ease can hide, or show menu bar items.
So, I would suggest you try this format:
So, not really sure how far "back" I need, and if there are folders before "views" then you include those.
As noted, much of this issue is that master page can be launched/used/enjoyed from ANY url, but the child .aspx page might be nested in any kind of nest folders. This means that your master menu bar can't really use relative addressing, since you have no idea where the master page actually is going to be run from.
So, while markup can't use "~/" to start from root, if you add a runat="server" tag, then you CAN use a path name starting from root with "~/".