My aspx pages arent found when debugging. ASP.NET Web Application using aspx pages (not MVC).
I have the following HTML code:
<Head title="WhycantIdebugbutafterdeploythisworks" url="#">
<MenuItem title="Some Menu" url="/ApplicationName/AFolder/First.aspx" />
<MenuItem title="Another Menu" url="/ApplicationName/AFolder/Second.aspx" /></Head>
After publish the aspx pages are found and display correctly: url=http://ApplicationName/AFolder/First.aspx
, but when I debug my app, it can't find the aspx pages and gives error message "The file '/ApplicationName/AFolder/First.aspx' does not exist."
. The debug url=https://localhost:44322/AFolder/First.aspx
.
If I remove /ApplicationName
from the url HTML attribute, the debug works but the deployed version does not.
How to fix the HTML so that the url works both in debug and on IIS?
CodePudding user response:
As a general rule, you don't include the "app name" in ANY of your url's.
You need to supply JUST the path name to the folder, and then the web page.
And you can specify "root" with a "~/" but ONLY if you have a runat="server" tag.
So, your urls can be say like this:
<MenuItem title="Some Menu" runat="server" url="~/AFolder/First.aspx" />
<MenuItem title="Another Menu" runat="server" url="~/AFolder/Second.aspx" />
Now, you can in most cases use just /Afolder/xxxx.aspx, however that WILL BE relative addressing from your current web page, and thus most menu's will not work if you sitting in a different locaiton or folder.
So, when possbile, try to add/use the runat="server" tag, and thus the system will resolve AND ALLOW use of the "~/".
The use of "~/" is not legal in javascript code, and it not in most cases for a simple hyper link.
However, for code behind such as response.Redirect("~/somefolder/xxxx.aspx"), the you are fine.
And for a hyper link control on a page (or menu)?
As noted, you can include/have/use the "~/" if the control in question has a runat="server" tag.
That way, the base root URL of the site will not matter, and MORE imporant should never matter.
So, since a asp.net control like a hyper link has the runat="server", then this URL is legal:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/Test/GridRadio.aspx">HyperLink</asp:HyperLink>
However, if you use a plain jane HTML link, then you can NOT use the "~/" in above. (and that means you have to take into account the folder you are in).
So, if I am in say folder Grids, and I want the above link?
I have this
http://localhostxxxx/Grids/MyGrid.aspx
And I want to navagate to
http://localhostxxxx/Test/GridRadio.aspx
Then a plane jane (non server) side like will look like this:
<a href="../Test/GridRadio.aspx">link text</a>
So, note how we have to "come down" one folder level with .., and then go back up to test folder.
However, with server side controls, as noted, you can (and should) always use the "~/folder/page.aspx" type of format, and it will always work for you.
So, don't include appname, don't include host name either, everything should ALWAYS be a path from your root folder, and in 99% of cases then, use "~/" with a server side control.
So, if you choose to "always" use relative addressing, then you MUST take into account the current page "nesting" as to where your current page sits, and the target page you want to navigate to.