本文整理汇总了C#中System.Net.Http.Headers.CacheControlHeaderValue类的典型用法代码示例。如果您正苦于以下问题:C# CacheControlHeaderValue类的具体用法?C# CacheControlHeaderValue怎么用?C# CacheControlHeaderValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CacheControlHeaderValue类属于System.Net.Http.Headers命名空间,在下文中一共展示了CacheControlHeaderValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
/// <summary>
/// Attempts to apply some caching rules to a response
/// </summary>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
var cacheHeaders = new CacheControlHeaderValue
{
NoCache = true
};
if ((int)response.StatusCode > 400 || // 400 and above are errors
response.StatusCode == HttpStatusCode.NoContent || // no content, no cache
response.StatusCode == HttpStatusCode.Created) // any 201 Created response is not cacheable as the response is based off of the request body
{
// no caching allowed
cacheHeaders.NoStore = true;
}
else if (response.StatusCode == HttpStatusCode.OK)
{
// 200 Okay responses should generally be cached
cacheHeaders.NoStore = false;
cacheHeaders.Private = false;
// always revalidate
cacheHeaders.MaxAge = new TimeSpan(0, 0, 0);
}
response.Headers.CacheControl = cacheHeaders;
return response;
}
示例2: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var cachecontrol = new CacheControlHeaderValue();
cachecontrol.MaxAge = TimeSpan.FromSeconds(_clientTimeSpan);
cachecontrol.MustRevalidate = true;
actionExecutedContext.ActionContext.Response.Headers.CacheControl = cachecontrol;
}
示例3: setClientCache
private CacheControlHeaderValue setClientCache()
{
var cachecontrol = new CacheControlHeaderValue();
cachecontrol.MaxAge = TimeSpan.FromSeconds(_clientTimeSpan);
cachecontrol.MustRevalidate = true;
return cachecontrol;
}
示例4: Subscribe
public void Subscribe(Uri subscriptionUri, Uri callbackUri, TimeSpan subscriptionTimeSpan)
{
var httpClient = new HttpClient();
var req = new HttpRequestMessage
{
RequestUri = subscriptionUri,
Version = HttpVersion.Version11
};
req.Headers.UserAgent.Add(new ProductInfoHeaderValue("SimpleSonos", "1.0"));
req.Method = new HttpMethod("SUBSCRIBE");
req.Headers.Add("CALLBACK", "<" + callbackUri.AbsoluteUri + ">");
req.Headers.Add("NT", "upnp:event");
req.Headers.Add("TIMEOUT", "Second-" + (int) subscriptionTimeSpan.TotalSeconds);
req.Headers.ConnectionClose = true;
var cch = new CacheControlHeaderValue {NoCache = true};
req.Headers.CacheControl = cch;
try
{
var resp = httpClient.SendAsync(req).Result;
Console.WriteLine("Gor response from " + subscriptionUri.Host + ". StatusCode: " + resp.StatusCode);
if (resp.Headers.Contains("SID"))
Console.WriteLine("SID: " + resp.Headers.GetValues("SID").First());
if (resp.Headers.Contains("TIMEOUT"))
Console.WriteLine("TIMEOUT: " + resp.Headers.GetValues("TIMEOUT").First());
if (resp.Headers.Contains("Server"))
Console.WriteLine("Server: " + resp.Headers.GetValues("Server").First());
}
catch (Exception e)
{
Console.WriteLine("ERROR TRYING TO SUBSCRIBE " + e);
}
}
示例5: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response.StatusCode >= HttpStatusCode.InternalServerError)
{
return;
}
var cachecontrol = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(ClientTimeSpan),
MustRevalidate = true,
Public = true
};
actionExecutedContext.Response.Headers.CacheControl = cachecontrol;
}
示例6: Should_be_determined_from_MaxAge_and_cacheControl
public bool Should_be_determined_from_MaxAge_and_cacheControl(int maxAge, bool? noCache, bool? noStore, int? cacheControlMaxAge, bool ignoreRevalidation)
{
CacheControlHeaderValue cacheControl = null;
if (noCache.HasValue || noStore.HasValue || cacheControlMaxAge.HasValue)
{
cacheControl = new CacheControlHeaderValue
{
NoCache = noCache ?? false,
NoStore = noStore ?? false,
};
if (cacheControlMaxAge.HasValue)
{
cacheControl.MaxAge = TimeSpan.FromSeconds(cacheControlMaxAge.Value);
}
}
var att = new OutputCacheAttributeWithPublicMethods
{
MaxAge = (uint)maxAge,
IgnoreRevalidationRequest = ignoreRevalidation
};
// Action
return att.ShouldIgnoreCachePublic(cacheControl, new HttpRequestMessage());
}
示例7: Should_return_null_if_cache_not_mature_as_min_fresh_request
public void Should_return_null_if_cache_not_mature_as_min_fresh_request()
{
// Arrange
var cacheControl = new CacheControlHeaderValue
{
MinFresh = TimeSpan.FromSeconds(100)
};
var cacheItem = new WebApiCacheItem
{
CreatedTime = DateTime.UtcNow.AddSeconds(-20),
MaxAge = 1000,
StaleWhileRevalidate = 5,
IgnoreRevalidationRequest = false
};
var request = new HttpRequestMessage();
var svc = new CacheResponseBuilder { };
// Action
var response = svc.GetResponse(cacheControl, cacheItem, request);
// Assert
Assert.IsNull(response);
}
示例8: NotModifiedResponse
public NotModifiedResponse(HttpRequestMessage request, CacheControlHeaderValue cacheControlHeaderValue, EntityTagHeaderValue etag)
: base(HttpStatusCode.NotModified)
{
if(etag!=null)
this.Headers.ETag = etag;
this.Headers.CacheControl = cacheControlHeaderValue;
this.RequestMessage = request;
}
示例9: CheckValidParsedValue
private void CheckValidParsedValue(string input, int startIndex, CacheControlHeaderValue expectedResult,
int expectedIndex)
{
HttpHeaderParser parser = CacheControlHeaderParser.Parser;
object result = null;
Assert.True(parser.TryParseValue(input, null, ref startIndex, out result));
Assert.Equal(expectedIndex, startIndex);
Assert.Equal(result, expectedResult);
}
示例10: HttpCacheControlPolicyAttribute
/// <summary>
/// default .ctor is no cache policy
/// </summary>
public HttpCacheControlPolicyAttribute()
{
_cacheControl = new CacheControlHeaderValue()
{
Private = true,
NoCache = true,
NoStore = true
};
}
示例11: SetClientCache
private CacheControlHeaderValue SetClientCache()
{
var cachecontrol = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(Duration),
MustRevalidate = true
};
return cachecontrol;
}
示例12: Properties_SetAndGetAllProperties_SetValueReturnedInGetter
public void Properties_SetAndGetAllProperties_SetValueReturnedInGetter()
{
CacheControlHeaderValue cacheControl = new CacheControlHeaderValue();
// Bool properties
cacheControl.NoCache = true;
Assert.True(cacheControl.NoCache);
cacheControl.NoStore = true;
Assert.True(cacheControl.NoStore);
cacheControl.MaxStale = true;
Assert.True(cacheControl.MaxStale);
cacheControl.NoTransform = true;
Assert.True(cacheControl.NoTransform);
cacheControl.OnlyIfCached = true;
Assert.True(cacheControl.OnlyIfCached);
cacheControl.Public = true;
Assert.True(cacheControl.Public);
cacheControl.Private = true;
Assert.True(cacheControl.Private);
cacheControl.MustRevalidate = true;
Assert.True(cacheControl.MustRevalidate);
cacheControl.ProxyRevalidate = true;
Assert.True(cacheControl.ProxyRevalidate);
// TimeSpan properties
TimeSpan timeSpan = new TimeSpan(1, 2, 3);
cacheControl.MaxAge = timeSpan;
Assert.Equal(timeSpan, cacheControl.MaxAge);
cacheControl.SharedMaxAge = timeSpan;
Assert.Equal(timeSpan, cacheControl.SharedMaxAge);
cacheControl.MaxStaleLimit = timeSpan;
Assert.Equal(timeSpan, cacheControl.MaxStaleLimit);
cacheControl.MinFresh = timeSpan;
Assert.Equal(timeSpan, cacheControl.MinFresh);
// String collection properties
Assert.NotNull(cacheControl.NoCacheHeaders);
Assert.Throws<ArgumentException>(() => { cacheControl.NoCacheHeaders.Add(null); });
Assert.Throws<FormatException>(() => { cacheControl.NoCacheHeaders.Add("invalid token"); });
cacheControl.NoCacheHeaders.Add("token");
Assert.Equal(1, cacheControl.NoCacheHeaders.Count);
Assert.Equal("token", cacheControl.NoCacheHeaders.First());
Assert.NotNull(cacheControl.PrivateHeaders);
Assert.Throws<ArgumentException>(() => { cacheControl.PrivateHeaders.Add(null); });
Assert.Throws<FormatException>(() => { cacheControl.PrivateHeaders.Add("invalid token"); });
cacheControl.PrivateHeaders.Add("token");
Assert.Equal(1, cacheControl.PrivateHeaders.Count);
Assert.Equal("token", cacheControl.PrivateHeaders.First());
// NameValueHeaderValue collection property
Assert.NotNull(cacheControl.Extensions);
Assert.Throws<ArgumentNullException>(() => { cacheControl.Extensions.Add(null); });
cacheControl.Extensions.Add(new NameValueHeaderValue("name", "value"));
Assert.Equal(1, cacheControl.Extensions.Count);
Assert.Equal(new NameValueHeaderValue("name", "value"), cacheControl.Extensions.First());
}
示例13: ApplyCacheHeaders
/// <summary>
/// Sets the cache headers
/// </summary>
/// <param name="response">The actual response</param>
protected virtual void ApplyCacheHeaders(
HttpResponseMessage response)
{
var cacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(ClientTimeSpan)
};
response.Headers.CacheControl = cacheControl;
}
示例14: HttpCacheControlPolicyAttribute
public HttpCacheControlPolicyAttribute(bool isPrivate, int maxAgeInSeconds)
: this()
{
_cacheControl = new CacheControlHeaderValue()
{
Private = isPrivate,
Public = !isPrivate,
MustRevalidate = true,
MaxAge = TimeSpan.FromSeconds(maxAgeInSeconds)
};
}
示例15: Should_return_GatewayTimeout_when_no_cache_and_request_sent_OnlyIfCached_control
public void Should_return_GatewayTimeout_when_no_cache_and_request_sent_OnlyIfCached_control()
{
// Arrange
var cacheControl = new CacheControlHeaderValue {OnlyIfCached = true};
var svc = new CacheResponseBuilder();
// Action
var result = svc.GetResponse(cacheControl, null, new HttpRequestMessage());
// Assert
Assert.AreEqual(HttpStatusCode.GatewayTimeout, result.StatusCode);
Assert.AreEqual("no cache available", result.Headers.GetValues("X-Flatwhite-Message").Single());
}