Cannot use a leading .. to exit above the top directory

When you use relative paths incorrectly in ASP.NET an exception will be thrown of the following error message: “Cannot use a leading .. to exit above the top directory”.

This usually occurs when you write a static url or generate a dynamic one with many upward levels back to the root directory like “../../”. So, while running the website the exception will be handled.

As an example, let’s say that you’re website contains the following link code section:

<!-- Style -->
<link rel="shortcut icon" href="icon.ico" />
<link href="../css/flags.css" rel='stylesheet' type='text/css'>
<asp:Image ImageUrl="../flag.png" />

What happens here is that your webpage is referring or want to access a content which is in the root folder or in a folder of one level up from the current webpage you’re standing at. Till now everything seems logical and true.

But the error occurs when this webpage is basically at the root level and no any upper level exists, where the content you want to access is either at the same level or at any other level in your website since you can’t skip or jump above the root level. So, the webpage won’t be able to refer to a content of one level up “../” since the page itself is at the root folder.

You have to always watch out the using of relative paths whether in HTML or in ASP.NET code like Server.Transfer or Server.Redirect since the request of a wrong relative path will cause a run time exception.