ASP.Net C# ResolveClientUrl 类内

发布于 2024-08-20 14:02:58 字数 555 浏览 5 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小草泠泠 2024-08-27 14:02:58

您还可以使用 VirtualPathUtility.ToAbsolute("~/home.aspx"); 来代替在 Page 对象(或任何控件)上调用 ResolveClientUrl,这将为您提供与调用 ResolveUrl("~/home.aspx"); 的结果相同

Instead of calling ResolveClientUrl on the Page object (or any controls), you can also use VirtualPathUtility.ToAbsolute("~/home.aspx"); which will give you the same result as calling ResolveUrl("~/home.aspx");

自此以后,行同陌路 2024-08-27 14:02:58

ResolveClientUrl 是 System.Web.UI.Control 类的成员,因此可以通过以下方式直接访问它:

var url = ResolveClientUrl("~/Some/Url/");

在 asp.net 页面的代码中调用时。

要在类中使用它,您必须将页面(或页面上的控件)传递到类的构造函数中。即使如此,我也不确定您是否能够按照您指示的方式使用它。您可能需要执行类似以下操作:

public class NavigationPath
{
  private string menuItems = string.Empty;

  public NavigationPath(Page page)
  {
    menuItems = "<li>" + "<a href=\"" + page.ResolveClientUrl("~/Home.aspx") + "\">home</a>" + "</li>";
  }
}

然后在您的 asp.net 页面中执行类似以下操作:

var navPath = new NavigationPage(this);

ResolveClientUrl is a member of the System.Web.UI.Control class, hence it's accessible directly as:

var url = ResolveClientUrl("~/Some/Url/");

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:

public class NavigationPath
{
  private string menuItems = string.Empty;

  public NavigationPath(Page page)
  {
    menuItems = "<li>" + "<a href=\"" + page.ResolveClientUrl("~/Home.aspx") + "\">home</a>" + "</li>";
  }
}

And then inside your asp.net page do something like:

var navPath = new NavigationPage(this);
可爱咩 2024-08-27 14:02:58

有点旧,但可能会对某人有所帮助。
使用:

using System.Web.UI;

并在代码中:

new Control().ResolveClientUrl("Path");

为我工作,但我使用Web应用程序而不是网站解决方案。

问候

Bit old but might help someone.
Using :

using System.Web.UI;

And in code:

new Control().ResolveClientUrl("Path");

Worked for me, I use Web Application and not Web Site solution, though.

Regards

疧_╮線 2024-08-27 14:02:58

我发现 VirtualPathUtility.ToAbsolute 非常适合我的目的。

工作完美:

protected void build_Menu() 
{

    StringBuilder sb = new StringBuilder();

    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/Default.aspx'>Home</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/CARS/Default.aspx'>Cars</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/AIRPLANES/Default.aspx'>Airplanes</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/MOTORCYCLES/Default.aspx'>Motorcycles</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/REPORTS/Default.aspx'>Reports</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/MANUALS/Default.aspx'>Manuals</a> </li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/ADMINISTRATION/Default.aspx'>Administration</a></li>"));


    MENUfromCodeBehind.Text = sb.ToString();

}

I found VirtualPathUtility.ToAbsolute to work very well for my purpose.

Worked perfectly:

protected void build_Menu() 
{

    StringBuilder sb = new StringBuilder();

    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/Default.aspx'>Home</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/CARS/Default.aspx'>Cars</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/AIRPLANES/Default.aspx'>Airplanes</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/MOTORCYCLES/Default.aspx'>Motorcycles</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/REPORTS/Default.aspx'>Reports</a></li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/MANUALS/Default.aspx'>Manuals</a> </li>"));
    sb.Append("<li><a href='" + VirtualPathUtility.ToAbsolute("~/ADMINISTRATION/Default.aspx'>Administration</a></li>"));


    MENUfromCodeBehind.Text = sb.ToString();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文