开发者

Relative URL not working in System.Web.UI.UserControl

开发者 https://www.devze.com 2023-02-08 06:46 出处:网络
public class foo : System.Web.UI.Control { public foo() { var a = new HyperLink(){ Text=\"Test\", NavigateUrl=\"~/abc.aspx\"};
public class foo : System.Web.UI.Control
{
    public foo()
    {
      var a = new HyperLink(){ Text="Test", NavigateUrl="~/abc.aspx"};
      this.Controls.Add(a);
    }
}

The above code works properly, and when added to a page will successfully identify the tilde / ~ symbol and convert the url into a relative url.

However, when I change the derivation of the class to System.Web.UI.WebControl it does absolutely nothing, and leaves the tilde / ~ intact.

I had a look at System.Web.UI.Control which implements the IUrlResolutionService interface, but still can't see开发者_开发知识库m to get System.Web.UI.WebControl to resolve urls.


You can try the System.Web.VirtualPathUtility class:

public foo()
{
  var a = new HyperLink()
      { 
          Text="Test", 
          NavigateUrl=VirtualPathUtility.ToAbsolute("~/abc.aspx")
      };
  this.Controls.Add(a);
}


I usually do this to get root and them map my path:

HostingEnvironment.ApplicationVirtualPath() + "/mypath/mypage.aspx"

0

精彩评论

暂无评论...
验证码 换一张
取 消