ASP.NET: Cannot use a leading .. to exit above the top directory

image

This is a really quick post, mostly for myself so the next time I have this issue I can find the answer (yes, I often search my blog before google).

The error “Cannot use a leading .. to exit above the top directory” can be thrown by ASP.NET when you use relative paths incorrectly.  If you generate a url with too many “../../../” levels in it that would take the user above the root directory, you can generate the exception.

Why the exception?  Security I guess, but it’s a client URL and the server should know better than let the internet walk the C drive, but at one time a company in Redmond had servers with just such issues.  My problem isn’t with the unneeded exception (after all, it would just be a 404 link worst case), but with the bug in ASP.NET that causes it.

If you are using Server.Transfer or HttpContext.RewritePath to redirect a request (say map it to a template page) and have a HyperLink control with the ImageUrl property set, you win an extra “../” by the framework.  The fix is to wrap the HyperLink control around an Image control. 

In code, if you have:

<asp:HyperLink runat="server" NavigateUrl="~/FlyPage.aspx"
ImageUrl="~/Images/DeadFly.png" Width="200" Height="200"/>

Change it to:

<asp:HyperLink runat="server" NavigateUrl="~/FlyPage.aspx">
<asp:Image runat="server" ImageUrl="~/Images/DeadFly.png"
Width="200" Height="200" />
</
asp:HyperLink>

(I said it was a quick post… now go register for CodeStock!)


Posted 03-12-2010 3:09 PM by Michael C. Neel
Filed under:

[Advertisement]

Comments

lex wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 03-12-2010 6:47 PM

hi,

can not understand problem... why not to use ~/ before image url?

"~/inc/img/some.jpg"  no extra "../" needed, framework do replace by itself. Also works great in master.pages...

tperri wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 03-12-2010 6:55 PM

Lex,

You cannot use ~/ because ~/ takes you to the root of the web application.  He was trying to go above the root with a ../

HTH

Michael C. Neel wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 03-13-2010 11:38 AM

Sorry, I sould have pulled out the bindings in the examples.  The image urls are actually "~/", but in processing them to html links for the client, the .Net framework will add too many "../" (I've always wondered why they chose relative paths by default)

DotNET News wrote DotNET News
on 03-15-2010 11:35 AM

DotNET News

theluk wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 09-12-2010 6:15 PM

Hey thanks, you helped me realy out...

Brian Dukes wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 03-14-2011 6:07 PM

Thanks, just ran into this.

MMH wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 04-01-2011 1:07 AM

Thanks for the post. Hyperlink fix

Harsha Vardhan wrote re: ASP.NET: Cannot use a leading .. to exit above the top directory
on 04-13-2011 2:13 PM

You are brilliant. It took me many hours to find this solution, (your article in Google).

You helped me a lot. God bless  :)

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)