Dave Konopka
Dave Konopka

Reputation: 1255

ResolveURL not resolving in a user control

I'm trying to use ResolveUrl() to set some paths in the code behind of a custom ASP.NET user control. The user control contains a navigation menu. I'm loading it on a page that's loading a master page.

When I call ResolveUrl("~") in my user control it returns "~" instead of the root of the site. When I call it in a page I get the root path as expected. I've stepped through with the debugger and confirmed, ResolveUrl("~") returns "~" in my user control code behind.

Is there some other way I should be calling the function in my user control code behind to get the root path of the site?

Upvotes: 3

Views: 4429

Answers (4)

Dave Konopka
Dave Konopka

Reputation: 1255

wonde's comment above led me to the answer --

I was attempting to use ResolveUrl before the control's page load fired. Therefore there was no context page for the function yet.

I moved my code into the page load function and now it's resolving as expected.

Thanks for the nudge in the right direction.

Upvotes: 3

BritishDeveloper
BritishDeveloper

Reputation: 13359

I suppose if it works on the page but not the control i guess you could try

this.Page.ResolveUrl("~");

Upvotes: 1

wonde
wonde

Reputation: 684

Take a look at this MSDN example. http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx. and Rick Strahl's Web Log http://www.west-wind.com/Weblog/posts/154812.aspx

Upvotes: 1

Fredou
Fredou

Reputation: 20118

look at System.Web.VirtualPathUtility.ToAbsolute.

Upvotes: 3

Related Questions