I have an ASHX into which I want to load a UserControl from a folder parallel to it. When I try to use a relative path using ..
, I get the exception, Cannot use a leading .. to exit above the top directory
. There are a lot of answers on this error here already, but this situation seems different.
Initially, during development, I loaded the control using ~/MyProject/Controls/MyControl.cs
, but in production, I found that the "~
" was different (I can't tell why, but it's a very old, very quirky setup, so I'm used to this kind of error).
First, I tried to use a relative path, ../Controls/MyControl.cs
. This failed with the error, 'Cannot use a leading .. to exit above the top directory`.
Then, I tried building the absolute path to the file, using Server.MapPath()
, but it seems that LoadControl()
doesn't like absolute paths.
I'm pretty confused with the ..
error, because the entire project is in a subfolder of the webroot, so you'd have to go a couple more levels deeper to go below the webroot. It is as if a generic web handler creates its own application space. I'm clearly misunderstanding something.
My abbreviated web folder structure looks kind of like this:
/ (webroot)
+ MyProject/
+ Controls/
MyControl.ascx
MyControl.ascx.cs
+ WebHandlers/
MyHandler.ashx
...and I want to load MyControl.ascx
into MyHandler.ashx
.