Correcting A .NET Server.Transfer, Resource Not Found Issue
I ran into an issue trying to utilize .NET’s Server.Transfer method attempting to pass variables from one form, located in a sub directory, to another form, located in a different sub directory. Passing .NET’s Server.Transfer method an absolute path of the desired location should have been enough to redirect the browser, but (of course) it wasn’t. Instead, I was being displayed a “Resource Not Found” error indicating that Server.Transfer wasn’t looking outside of the current sub directory in an attempt to resolve the path the path. For example, if I was trying to access a file from my first sub directory located within my second sub directory via:
' Code called from ~/ApplicationRoot/FirstSubDirectory/myFirstPage.aspx Server.Transfer("~/ApplicationRoot/SecondSubDirectory/mySecondPage.aspx")
the error would say it’s was looking for the file in the current sub directory: “/ApplicaitonRoot/FirstSubDirectory/mySecondPage.aspx”. For some reason, Server.Transfer wasn’t leaving the current directory before attempting to find the appropriate file.
After a lot of time on Bing, and a day’s worth of trial and error, it appears the Server.Transfer was basing my application’s relative root path off of the form’s action attribute. That is, because the form submits to itself, I hadn’t included an absolute path in the attribute; I was simply using the page’s name.
<!-- Prevented Server.Transfer from finding my application's root --> <form id="myFormID" runat="server" method="post" action="myFirstPage.aspx"> <!-- Allowed Server.Transfer to find my application's root. --> <form id="myFormID" runat="server" method="post" action="/ApplicationRoot/FirstSubDirectory/myFirstPage.aspx">
Once the action was modified to contain the absolute path of the page, the Server.Transfer method functioned as expected.
To be honest I’m not sure why this worked. I can’t find anything in the MSDN libraries that explain this behavior. If someone could explain it be me, I would appreciate it.