Control.ResolveUrl(String) Método
Definição
Converte uma URL em uma que possa ser usada no cliente solicitante.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
Parâmetros
- relativeUrl
- String
A URL associada à propriedade TemplateSourceDirectory.The URL associated with the TemplateSourceDirectory property.
Retornos
A URL convertida.The converted URL.
Exceções
Ocorrerá se o parâmetro relativeUrl contiver null.Occurs if the relativeUrl parameter contains null.
Exemplos
O exemplo a seguir cria um Image objeto de controle de servidor Web e usa o ResolveUrl método para definir o caminho para a imagem, que é armazenada pela ImageUrl propriedade.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
Comentários
Se o relativeUrl parâmetro contiver uma URL absoluta, a URL será retornada inalterada.If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. Se o relativeUrl parâmetro contiver uma URL relativa, essa URL será alterada para uma URL relativa que esteja correta para o caminho de solicitação atual, para que o navegador possa resolver a 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.
Por exemplo, considere o seguinte cenário:For example, consider the following scenario:
Um cliente solicitou uma página ASP.NET que contém um controle de usuário que tem uma imagem associada a ela.A client has requested an ASP.NET page that contains a user control that has an image associated with it.
A página ASP.NET está localizada em/Store/page1.aspx.The ASP.NET page is located at /Store/page1.aspx.
O controle de usuário está localizado em/Store/UserControls/UC1.ascx.The user control is located at /Store/UserControls/UC1.ascx.
O arquivo de imagem está localizado em/UserControls/Images/Image1.jpg.The image file is located at /UserControls/Images/Image1.jpg.
Se o controle de usuário passar o caminho relativo para a imagem (ou seja,/Store/UserControls/Images/Image1.jpg) para o ResolveUrl método, o método retornará o valor/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.
Esse método usa a TemplateSourceDirectory propriedade para resolver a URL absoluta.This method uses the TemplateSourceDirectory property to resolve to the absolute URL. A URL retornada é para uso do cliente.The returned URL is for client use.
Para obter mais informações sobre caminhos de recurso em um site, consulte caminhos de projeto web ASP.net.For more information on resource paths in a Web site, see ASP.NET Web Project Paths.
Observação
Somente para páginas da Web móveis, se seu aplicativo depende de sessões sem cookie ou pode receber solicitações de navegadores móveis que exigem sessões sem cookie, o uso de um til (" ~ ") em um caminho pode resultar na criação inadvertida de uma nova sessão e na perda dos dados da sessão.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. Para definir uma propriedade com um caminho como " ~ / Path", resolva o caminho chamando o ResolveUrl com um argumento como " ~ / Path" antes de atribuí-lo à propriedade.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.