Skip to content

OrRequestCondition

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

Or Request Condition

Summary

The OrRequestCondition object is used to combine multiple existing IRequestCondition implementations together, where requests matching ANY of the provided conditions are processed.

Samples

This sample uses the OrRequestCondition to combine the AuthorityRequestCondition
and RequestContentMediaTypeCondition. The 'or condition' is then used with the Compressed Requests Handler so requests to www.mydomain.com OR with a plain text content are compressed.

// Construct an authority condition
var authorityCondition = new AuthorityRequestCondition();
authorityCondition.AddAuthority("www.sometestsite.com");

// Construct a media type condition
var mediaTypeCondition = new RequestContentMediaTypeCondition();
mediaTypeCondition.AddContentMediaType(MediaTypes.TextPlain);

// Create the 'or condition' requiring both previous conditions
// to be true.
var orCondition = new OrRequestCondition(new IRequestCondition[] { authorityCondition, mediaTypeCondition });

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

// Send a request that will be compressed because the request authority matches the conditions.
var uncompressedContent = "<html><head><title>test</title></head><body>test page</body></html>";
var result = await client.PostAsync("http://www.mydomain.com/SomeEndPoint", 
    new System.Net.Http.StringContent
    (
        uncompressedContent, 
        System.Text.UTF8Encoding.UTF8, 
        MediaTypes.TextHtml
    )
).ConfigureAwait(false);

// Send a request that will be compressed because the media type matches the conditions.
uncompressedContent = "A compressible string. A compressible string. A compressible string. A compressible string.";
result = await client.PostAsync("http://www.someotherdomain.com/SomeEndPoint", 
    new System.Net.Http.StringContent
    (
        uncompressedContent, 
        System.Text.UTF8Encoding.UTF8, 
        MediaTypes.TextPlain
    )
).ConfigureAwait(false);

// This request will NOT be compressed as neither condition matches the filter.
uncompressedContent = "<html><head><title>test</title></head><body>test page</body></html>";
result = await client.PostAsync("http://www.someotherdomain.com/SomeEndPoint", 
    new System.Net.Http.StringContent
    (
        uncompressedContent, 
        System.Text.UTF8Encoding.UTF8, 
        MediaTypes.TextHtml
    )
).ConfigureAwait(false);