Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
Reference
System.Web.UI
Control Class
Methods
 ResolveUrl Method
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
.NET Framework Class Library
Control.ResolveUrl Method

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

[Visual Basic]
Public Function ResolveUrl( _
   ByVal relativeUrl As String _
) As String
[C#]
public string ResolveUrl(
 string relativeUrl
);
[C++]
public: String* ResolveUrl(
 String* relativeUrl
);
[JScript]
public function ResolveUrl(
   relativeUrl : String
) : String;

Parameters

relativeUrl
The URL associated with the TemplateSourceDirectory property.

Return Value

The converted URL.

Exceptions

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

Remarks

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/MyPhoto1.jpg.

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

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

Example

[Visual Basic, C#] 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.

[Visual Basic] 
Public Class MyResolveUrl
   Inherits Control
   Private _ImageUrl As String

   Public Property ImageUrl() As String
      Get
         Return _ImageUrl
      End Get
      Set
         _ImageUrl = value
      End Set
   End Property

   Protected Overrides Sub Render(output As HtmlTextWriter)
      Dim myImage As New System.Web.UI.WebControls.Image()
      ' Resolve Url.
      myImage.ImageUrl = ResolveUrl(Me.ImageUrl)
      myImage.RenderControl(output)
   End Sub
End Class

[C#] 
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);         
   }     
}

[Visual Basic, C#]

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | TemplateSourceDirectory

© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker