简体   繁体   English

ResolveURL无法在用户控件中解析

[英]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. 我试图使用ResolveUrl()在自定义ASP.NET用户控件后面的代码中设置一些路径。 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. 当我在用户控件中调用ResolveUrl(“〜”)时,它将返回“〜”而不是站点的根目录。 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. 我已逐步调试,并确认,ResolveUrl(“〜”)在后面的用户控制代码中返回“〜”。

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? 我还有其他方法可以在后面的用户控制代码中调用该函数以获取网站的根路径吗?

wonde's comment above led me to the answer -- 上面的翁德(Wonde)评论使我得到了答案-

I was attempting to use ResolveUrl before the control's page load fired. 在尝试触发控件的页面加载之前,我试图使用ResolveUrl。 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. 感谢您朝着正确的方向前进。

我想如果它能在页面上正常运行,但不能在控件上运行,我想您可以尝试

this.Page.ResolveUrl("~");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM