共用方式為


HttpContentHeaderCollection.ContentType 屬性

定義

取得或設定 HttpMediaTypeHeaderValue 物件,代表 HTTP 內容上 HTTP Content-Type 標頭的值。

public:
 property HttpMediaTypeHeaderValue ^ ContentType { HttpMediaTypeHeaderValue ^ get(); void set(HttpMediaTypeHeaderValue ^ value); };
HttpMediaTypeHeaderValue ContentType();

void ContentType(HttpMediaTypeHeaderValue value);
public HttpMediaTypeHeaderValue ContentType { get; set; }
var httpMediaTypeHeaderValue = httpContentHeaderCollection.contentType;
httpContentHeaderCollection.contentType = httpMediaTypeHeaderValue;
Public Property ContentType As HttpMediaTypeHeaderValue

屬性值

物件,表示 HTTP 內容上 HTTP Content-Type 標頭的值。 Null值表示標頭不存在。

備註

ContentType 屬性代表 HTTP 內容的 Content-Type 標頭值。 Content-Type標頭是內容的 MIME 類型。

下列範例程式碼示範在 HTTP 內容上使用HttpContentHeaderCollection物件上的 ContentType 屬性取得或設定Content-Type標頭值的方法。

// Content-Type header
// HttpMediaTypeHeaderValue (MediaType, Charset are strings, Parameters is an IList<HttpNameValueHeaderValue>)
//
void DemoContentType(IHttpContent content) {
    var h = content.Headers;
    h.ContentType = new HttpMediaTypeHeaderValue("myMediaType");

    var header = h.ContentType;
    uiLog.Text += "\nCONTENT TYPE HEADER\n";

    // Parameters is an IList<HttpNameValueHeaderValue> of Name/Value strings
    var parameterString = "";
    foreach (var parameter in header.Parameters) {
            parameterString += string.Format("[{0}={1}] ", parameter.Name, parameter.Value);
    }
    if (parameterString == "") {
            parameterString = "(no parameters)";
    }

    uiLog.Text += string.Format("Content-Type: MediaType: {0} CharSet: {1} Parameters: {2} ToString: {3}\n", header.MediaType, header.CharSet, parameterString, header.ToString());
}

適用於

另請參閱