maxp
maxp

Reputation: 25161

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

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 seem to get System.Web.UI.WebControl to resolve urls.

Upvotes: 2

Views: 387

Answers (2)

Shekhar_Pro
Shekhar_Pro

Reputation: 18430

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

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

Upvotes: 1

JK.
JK.

Reputation: 21825

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);
}

Upvotes: 1

Related Questions