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. 若要设置具有路径(如 "~/path")的属性,请在将该 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.