Skip to content

RequestContentMediaTypeCondition

Troy Willmot edited this page Sep 25, 2016 · 2 revisions

Request Content Media Type Condition

Summary

The RequestContentMediaTypeCondition is an implementation of the IRequestCondition interface. It allows processing only requests where the MediaType property of the ContentType content header is one of a specific set of values.

Samples

This sample uses the RequestContentMediaTypeCondition with the Compressed Requests Handler so only requests with a content media type of text/plain or application/xml are compressed.

// Create the condition object with the allowed list of media types.
var mediaTypeCondition = new RequestContentMediaTypeCondition(
    new string[] 
    {
        "text/plain",
        "application/xml"
    }    
);

// Construct the compressed request handler with the condition object attached.
var handler = new CompressedRequestHandler(mh, mediaTypeCondition);
var client = new System.Net.Http.HttpClient(handler);

// Now send a request that will be compressed because the media type is text/plain 
var uncompressedContent = "A compressible string. A compressible string. A compressible string. A compressible string.";
var result = await client.PostAsync("http://www.mydomain.com/SomeEndPoint", 
    new System.Net.Http.StringContent(uncompressedContent, UTF8Encoding.UTF8, MediaTypes.TextPlain)
).ConfigureAwait(false);

Clone this wiki locally