Control.ResolveUrl(String) 方法


將 URL 轉換為要求用戶端可使用的 URL。Converts a URL into one that is usable on the requesting client.

 System::String ^ ResolveUrl(System::String ^ relativeUrl);
public string ResolveUrl (string relativeUrl);
member this.ResolveUrl : string -> string
Public Function ResolveUrl (relativeUrl As String) As String



TemplateSourceDirectory 屬性關聯的 URL。The URL associated with the TemplateSourceDirectory property.


已轉換的 URL。The converted URL.


發生在 relativeUrl 參數包含 null 的情況。Occurs if the relativeUrl parameter contains null.


下列範例會建立一個 Image Web 服務器控制項物件,並使用 ResolveUrl 方法來設定影像的路徑,而這是由 ImageUrl 屬性所儲存。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
         return _ImageUrl;
         _ImageUrl = value;
   protected override void Render(HtmlTextWriter output)
      Image myImage = new Image();
      // Resolve Url.
      myImage.ImageUrl = ResolveUrl(this.ImageUrl);
Public Class MyResolveUrl
   Inherits Control
   Private _ImageUrl As String

   Public Property ImageUrl() As String
         Return _ImageUrl
      End Get
         _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)
   End Sub
End Class


如果 relativeUrl 參數包含絕對 URL,則 URL 會原封不動地傳回。If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. 如果 relativeUrl 參數包含相對的 URL,該 URL 會變更為對目前要求路徑而言正確的相對 URL,讓瀏覽器可以解析 URL。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:

  • 用戶端已要求 ASP.NET 網頁,其中包含具有相關聯影像的使用者控制項。A client has requested an ASP.NET page that contains a user control that has an image associated with it.

  • ASP.NET 網頁位於/Store/page1.aspx。The ASP.NET page is located at /Store/page1.aspx.

  • 使用者控制項位於/Store/UserControls/UC1.ascx。The user control is located at /Store/UserControls/UC1.ascx.

  • 影像檔案位於/UserControls/Images/Image1.jpg。The image file is located at /UserControls/Images/Image1.jpg.

如果使用者控制項將影像的相對路徑(也就是/Store/UserControls/Images/Image1.jpg)傳遞給 ResolveUrl 方法,此方法會傳回值/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.

這個方法會使用 TemplateSourceDirectory 屬性來解析為絕對 URL。This method uses the TemplateSourceDirectory property to resolve to the absolute URL. 傳回的 URL 是供用戶端使用。The returned URL is for client use.

如需網站中資源路徑的詳細資訊,請參閱ASP.NET Web 專案路徑For more information on resource paths in a Web site, see ASP.NET Web Project Paths.


僅適用于行動網頁,如果您的應用程式依賴無 cookie 會話,或可能收到需要無 cookie 會話的行動瀏覽器要求,在路徑中使用波狀符號("~")可能會導致不小心建立新的會話,而且可能會發生遺失會話資料。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. 若要使用「~/路徑」之類的路徑來設定屬性,請使用引數呼叫 ResolveUrl 來解析路徑,例如 "~/path" ,然後再將它指派給屬性。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.