HtmlTextWriter.EncodeUrl(String) HtmlTextWriter.EncodeUrl(String) HtmlTextWriter.EncodeUrl(String) HtmlTextWriter.EncodeUrl(String) Method

定義

執行最少的 URL 編碼,透過的方式是將指定 URL 中的空格轉換為字串 "%20"。Performs minimal URL encoding by converting spaces in the specified URL to the string "%20".

protected:
 System::String ^ EncodeUrl(System::String ^ url);
protected string EncodeUrl (string url);
member this.EncodeUrl : string -> string
Protected Function EncodeUrl (url As String) As String

參數

url
String String String String

字串,包含要編碼的 URL。A string containing the URL to encode.

傳回

字串,包含已編碼的 URL。A string containing the encoded URL.

範例

下列程式碼範例示範如何呼叫EncodeUrl方法,以轉換在AddAttribute方法呼叫中當做參數傳遞的 URL 中的任何空格。The following code example demonstrates how to call the EncodeUrl method to convert any spaces in the URL that is passed as a parameter in an AddAttribute method call.

// If an <anchor> element is rendered and an href
// attribute has not been defined, call the AddAttribute
// method to add an href attribute
// and set it to http://www.cohowinery.com.
// Use the EncodeUrl method to convert any spaces to %20.
if ( TagKey == HtmlTextWriterTag::A )
{
   if (  !IsAttributeDefined( HtmlTextWriterAttribute::Href ) )
   {
      AddAttribute( "href", EncodeUrl( "http://www.cohowinery.com" ) );
   }
}
// If an <anchor> element is rendered and an href
// attribute has not been defined, call the AddAttribute
// method to add an href attribute
// and set it to http://www.cohowinery.com.
// Use the EncodeUrl method to convert any spaces to %20.
if (TagKey == HtmlTextWriterTag.A)
{
    if (!IsAttributeDefined(HtmlTextWriterAttribute.Href))
    {
        AddAttribute("href", EncodeUrl("http://www.cohowinery.com"));
    }
}
' If an <anchor> element is rendered and an href
' attribute has not been defined, call the AddAttribute
' method to add an href attribute
' and set it to http://www.cohowinery.com.
' Use the EncodeUrl method to convert any spaces to %20.
If TagKey = HtmlTextWriterTag.A Then
    If Not IsAttributeDefined(HtmlTextWriterAttribute.Href) Then
        AddAttribute("href", EncodeUrl("http://www.cohowinery.com"))
    End If
End If

備註

字元的 URL 編碼包含百分比符號(%),後面接著字元的兩位數十六進位標記法(不區分大小寫)。URL encoding of a character consists of a percent symbol (%), followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character. 空格的十六進位表示為20。The hexadecimal representation of a space is 20.

適用於

另請參閱