Control.ResolveUrl(String) Control.ResolveUrl(String) Control.ResolveUrl(String) Control.ResolveUrl(String) Method

定義

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

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

參數

relativeUrl
String String String String

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

傳回

已轉換的 URL。The converted URL.

例外狀況

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

範例

下列範例會建立ImageWeb 伺服器控制項物件,並使用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
   {
      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
   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

備註

如果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.

注意

Mobile Web 網頁,如果您的應用程式依賴無 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這類的引數 」~/路徑" 再將它指派給屬性。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.

適用於

另請參閱