WebBrowser.Url Propiedad

Definición

Obtiene o establece la dirección URL del documento actual.

public:
 property Uri ^ Url { Uri ^ get(); void set(Uri ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri Url { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri? Url { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))>]
member this.Url : Uri with get, set
Public Property Url As Uri

Valor de propiedad

Uri

Uri que representa la dirección URL del documento actual.

Atributos

Excepciones

Esta instancia de WebBrowser ya no es válida.

Una referencia a una implementación de la interfaz IWebBrowser2 no se pudo recuperar del control ActiveX WebBrowser subyacente.

El valor especificado al establecer esta propiedad no es un URI absoluto. Para obtener más información, vea IsAbsoluteUri.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la Url propiedad para implementar una barra de direcciones para el WebBrowser control. En este ejemplo se requiere que el formulario contenga un control denominado webBrowser1, un TextBox control denominado TextBoxAddressy un Button control denominado ButtonGo.WebBrowser Al escribir una dirección URL en el cuadro de texto y presionar ENTRAR o hacer clic en el botón Ir , el WebBrowser control navega a la dirección URL especificada. Al navegar haciendo clic en un hipervínculo, el cuadro de texto se actualiza automáticamente para mostrar la dirección URL actual.

// Navigates to the URL in the address text box when 
// the ENTER key is pressed while the text box has focus.
void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
   if ( e->KeyCode == System::Windows::Forms::Keys::Enter &&  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Navigates to the URL in the address text box when 
// the Go button is clicked.
void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   if (  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Updates the URL in TextBoxAddress upon navigation.
void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ )
{
   this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();
}
// Navigates to the URL in the address box when 
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Navigate(toolStripTextBox1.Text);
    }
}

// Navigates to the URL in the address box when 
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
    Navigate(toolStripTextBox1.Text);
}

// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

' Navigates to the URL in the address box when 
' the ENTER key is pressed while the ToolStripTextBox has focus.
Private Sub toolStripTextBox1_KeyDown( _
    ByVal sender As Object, ByVal e As KeyEventArgs) _
    Handles toolStripTextBox1.KeyDown

    If (e.KeyCode = Keys.Enter) Then
        Navigate(toolStripTextBox1.Text)
    End If

End Sub

' Navigates to the URL in the address box when 
' the Go button is clicked.
Private Sub goButton_Click( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles goButton.Click

    Navigate(toolStripTextBox1.Text)

End Sub

' Navigates to the given URL if it is valid.
Private Sub Navigate(ByVal address As String)

    If String.IsNullOrEmpty(address) Then Return
    If address.Equals("about:blank") Then Return
    If Not address.StartsWith("http://") And _
        Not address.StartsWith("https://") Then
        address = "http://" & address
    End If

    Try
        webBrowser1.Navigate(New Uri(address))
    Catch ex As System.UriFormatException
        Return
    End Try

End Sub

' Updates the URL in TextBoxAddress upon navigation.
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
    ByVal e As WebBrowserNavigatedEventArgs) _
    Handles webBrowser1.Navigated

    toolStripTextBox1.Text = webBrowser1.Url.ToString()

End Sub

Comentarios

Establecer esta propiedad equivale a llamar al Navigate método y pasarla la dirección URL especificada.

El WebBrowser control mantiene una lista de historial de todas las páginas web visitadas durante una sesión de exploración. Al establecer la Url propiedad , el WebBrowser control navega a la dirección URL especificada y la agrega al final de la lista de historial.

El WebBrowser control almacena páginas web de sitios visitados recientemente en una memoria caché en el disco duro local. Cada página puede especificar una fecha de expiración que indique cuánto tiempo permanecerá en la memoria caché. Cuando el control navega a una página, ahorra tiempo mostrando una versión almacenada en caché, si hay una disponible, en lugar de volver a descargar la página. Use el Refresh método para forzar que el WebBrowser control vuelva a cargar la página actual descargóla, asegurándose de que el control muestre la versión más reciente.

Nota

Esta propiedad contiene la dirección URL del documento actual, incluso si se ha solicitado otro documento. Si establece el valor de esta propiedad y, a continuación, vuelve a recuperarlo inmediatamente, el valor recuperado puede ser diferente del conjunto de valores si el WebBrowser control no ha tenido tiempo para cargar el nuevo documento. Puede recuperar el nuevo valor en un DocumentCompleted controlador de eventos.

Se aplica a

Consulte también