HtmlTextWriter.EncodeUrl(String) 方法

定義

執行最少的 URL 編碼,透過的方式是將指定 URL 中的空格轉換為字串 "%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

字串,包含要編碼的 URL。

傳回

String

字串,包含已編碼的 URL。

範例

下列程式碼範例示範如何呼叫 EncodeUrl 方法,以轉換 URL 中傳遞為方法呼叫中 AddAttribute 參數的任何空格。

// 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 編碼是由百分比符號 (%) 所組成,後面接著字元的兩位數十六進位標記法 (不區分大小寫) 字元的ISO-Latin代碼點。 空間的十六進位標記法是 20。

適用於

另請參閱