ASP.Net C# ResolveClientUrl 类内
我有以下代码:
public class NavigationPath
{
private string menuItems = "<li>" +
"<a href=\"#\">home</a>" +
"</li>";
但我想要:
public class NavigationPath
{
private string menuItems = "<li>" +
"<a href=\"" + ResolveClientUrl("~/home.aspx") + "\">re</a>" +
"</li>";
但是,我无法在类中使用 ResolveClientUrl。有什么想法吗?
I have the following code:
public class NavigationPath
{
private string menuItems = "<li>" +
"<a href=\"#\">home</a>" +
"</li>";
But I would like to have:
public class NavigationPath
{
private string menuItems = "<li>" +
"<a href=\"" + ResolveClientUrl("~/home.aspx") + "\">re</a>" +
"</li>";
However, I am not able to use ResolveClientUrl inside a Class. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 VirtualPathUtility.ToAbsolute("~/home.aspx"); 来代替在 Page 对象(或任何控件)上调用 ResolveClientUrl,这将为您提供与调用 ResolveUrl("~/home.aspx"); 的结果相同
Instead of calling
ResolveClientUrl
on the Page object (or any controls), you can also useVirtualPathUtility.ToAbsolute("~/home.aspx");
which will give you the same result as callingResolveUrl("~/home.aspx");
ResolveClientUrl 是 System.Web.UI.Control 类的成员,因此可以通过以下方式直接访问它:
在 asp.net 页面的代码中调用时。
要在类中使用它,您必须将页面(或页面上的控件)传递到类的构造函数中。即使如此,我也不确定您是否能够按照您指示的方式使用它。您可能需要执行类似以下操作:
然后在您的 asp.net 页面中执行类似以下操作:
ResolveClientUrl is a member of the System.Web.UI.Control class, hence it's accessible directly as:
when called within the code of your asp.net page.
To use it inside a class you're going to have to pass the Page (or a control on the page) into the class in its constructor. Even then I'm not sure you'd be able to use it in the way you've indicated. You'd probably have to do something similar to:
And then inside your asp.net page do something like:
有点旧,但可能会对某人有所帮助。
使用:
并在代码中:
为我工作,但我使用Web应用程序而不是网站解决方案。
问候
Bit old but might help someone.
Using :
And in code:
Worked for me, I use Web Application and not Web Site solution, though.
Regards
我发现 VirtualPathUtility.ToAbsolute 非常适合我的目的。
工作完美:
I found VirtualPathUtility.ToAbsolute to work very well for my purpose.
Worked perfectly: