Click here to Skip to main content
11,929,481 members (52,128 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

ASP.NET ResolveUrl Without Page

, 2 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Using ResolveUrl without Page.

The quick answer


Don't forget the ~/ (tilde) before the page name.

The lengthy explanation

Assume that we want to redirect to default.aspx that's on the site root:


If we're on a page that's also on the root, it will map to and work correctly.

The problem

The problem comes when we're not on the site root but inside a child folder. Let's say we're on a page that's inside the /Administration folder, the same line will map to and that of course won't resolve and return a glorious 404: Page Not Found.

The solution

Solving this is easy using the ResolveUrl method:


Where this won't work

ResolveUrl is only available within the context of a Page or a UserControl; if you're, for instance, inside an ASHX, this method isn't available. In these cases, you have to use the following line to do the job:


We must include the tilde before the page name to make it a relative path, otherwise this method will throw an exception.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.

Find more at on my blog.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
prabhu singh17-Feb-13 19:11
memberprabhu singh17-Feb-13 19:11 
GeneralMy vote of 5 Pin
Alex B. Clarke16-Nov-12 1:42
memberAlex B. Clarke16-Nov-12 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 2 Jun 2011
Article Copyright 2011 by AlexCode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid