QueryStringConverter 類別

定義

這個類別會將查詢字串中的參數轉換成適當型別的物件, 也可以將參數從物件轉換成為它的查詢字串表示。

public ref class QueryStringConverter
public class QueryStringConverter
type QueryStringConverter = class
Public Class QueryStringConverter
繼承
QueryStringConverter
衍生

範例

下列程式碼會示範如何使用 QueryStringConverter 類別,在字串和 32 位元整數之間進行轉換。

QueryStringConverter converter = new QueryStringConverter();
if (converter.CanConvert(typeof(Int32)))
    converter.ConvertStringToValue("123", typeof(Int32));
int value = 321;
string strValue = converter.ConvertValueToString(value, typeof(Int32));
Console.WriteLine("the value = {0}, the string representation of the value = {1}", value, strValue);
Dim converter As New QueryStringConverter()
If (converter.CanConvert(GetType(Int32))) Then
    converter.ConvertStringToValue("123", GetType(Int32))
End If

Dim value As Integer = 321
Dim strValue As String = converter.ConvertValueToString(value, GetType(Int32))
Console.WriteLine("the value = {0}, the string representation of the value = {1}", value, strValue)

備註

參數可以在 URL 內的查詢字串中指定。 這個類別採用在字串中指定的那些參數,並且將它們轉換成物件。 例如,下列合約會經過定義。

[ServiceContract]  
interface Calculator  
{  
   [WebGet(UriTemplate="Add?n1={n1}&n2={n2}")]  
   [OperationContract]  
   long Add(long n1, long n2);  
}  

Windows Communication Foundation (WCF) 服務會實作此介面,並在端點上公開它,其 WebHttpBehaviorhttp://localhost:8000/MyCalcService 位於 。 藉 Add 由將 HTTP GET 傳送至 http://localhost:8000/MyCalcService/Add?n1=10&n2=5 ,即可呼叫服務作業。 QueryStringConverter 會接收此 URL,並將 URL 中指定的兩個參數 (n1 和 n2) 轉換成兩個有適當值的 long 物件。

您可以從 QueryStringConverter 衍生類別,以控制查詢字串參數如何對應至服務作業的參數。

根據預設,QueryStringConverter 支援下列型別:

建構函式

QueryStringConverter()

初始化 QueryStringConverter 類別的新執行個體。

方法

CanConvert(Type)

判斷指定的型別和字串表示之間是否可以相互轉換。

ConvertStringToValue(String, Type)

將查詢字串參數轉換成指定的型別。

ConvertValueToString(Object, Type)

將參數轉換成查詢字串表示。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於