Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Control.ResolveUrl Method

Converts a URL into one that is usable on the requesting client.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

public string ResolveUrl (
	string relativeUrl
)
public String ResolveUrl (
	String relativeUrl
)
public function ResolveUrl (
	relativeUrl : String
) : String
Not applicable.

Parameters

relativeUrl

The URL associated with the TemplateSourceDirectory property.

Return Value

The converted URL.

Exception typeCondition

ArgumentNullException

Occurs if the relativeUrl parameter contains a null reference (Nothing in Visual Basic).

If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL.

For example, consider the following scenario:

  • A client has requested an ASP.NET page that contains a user control that has an image associated with it.

  • The ASP.NET page is located at /Store/page1.aspx.

  • The user control is located at /Store/UserControls/UC1.ascx.

  • The image file is located at /UserControls/Images/Image1.jpg.

If the user control passes the relative path to the image (that is, /Store/UserControls/Images/Image1.jpg) to the ResolveUrl method, the method will return the value /Images/Image1.jpg.

This method uses the TemplateSourceDirectory property to resolve to the absolute URL. The returned URL is for client use.

For more information on resource paths in a Web site, see ASP.NET Web Site Paths.

NoteNote:

For mobile Web pages only, if your application relies on cookieless sessions or might receive requests from mobile browsers that require cookieless sessions, using a tilde ("~") in a path can result in inadvertently creating a new session and potentially losing session data. To set a property with a path such as "~/path", resolve the path by calling the ResolveUrl with an argument such as "~/path" before assigning it to the property.

The following example creates an Image Web server control object and uses the ResolveUrl method to set the path to the image, which is stored by the ImageUrl property.

public class MyResolveUrl:Control
{
   private string _ImageUrl;     
   public string ImageUrl
   {
      get
      {
         return _ImageUrl;
      }
      set
      {
         _ImageUrl = value;
      }
   }
   protected override void Render(HtmlTextWriter output)
   {           
      Image myImage = new Image();
      // Resolve Url.
      myImage.ImageUrl = ResolveUrl(this.ImageUrl);
      myImage.RenderControl(output);         
   }     
}

public class MyResolveUrl extends Control
{
    private String _ImageUrl;

    /** @property 
     */
    public String get_ImageUrl()
    {
        return _ImageUrl;
    } //get_ImageUrl

    /** @property 
     */
    public void set_ImageUrl(String value)
    {
        _ImageUrl = value;
    } //set_ImageUrl

    protected void Render(HtmlTextWriter output)
    {
        Image myImage = new Image();

        // Resolve Url.
        myImage.set_ImageUrl(ResolveUrl(this.get_ImageUrl()));
        myImage.RenderControl(output);
    } //Render
} //MyResolveUrl

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft