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.

示例

下面的示例创建一个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
   {
      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.

备注

仅适用于移动网页时,如果应用程序依赖于无 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")的属性,请在将其分配给属性之前,通过使用~参数(如 "/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.

适用于

另请参阅