本文整理汇总了C#中System.Net.Http.Headers.CacheControlHeaderValue类的典型用法代码示例。如果您正苦于以下问题:C# CacheControlHeaderValue类的具体用法?C# CacheControlHeaderValue怎么用?C# CacheControlHeaderValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CacheControlHeaderValue类属于System.Net.Http.Headers命名空间,在下文中一共展示了CacheControlHeaderValue类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:bradzacher,项目名称:Brewmaster,代码行数:28,代码来源:CacheHandler.cs
示例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;
}
开发者ID:james-wu,项目名称:webapi.inmemory,代码行数:7,代码来源:OutputCaching.cs
示例3: setClientCache
private CacheControlHeaderValue setClientCache()
{
var cachecontrol = new CacheControlHeaderValue();
cachecontrol.MaxAge = TimeSpan.FromSeconds(_clientTimeSpan);
cachecontrol.MustRevalidate = true;
return cachecontrol;
}
开发者ID:gregpakes,项目名称:aspnetwebapi-outputcache,代码行数:7,代码来源:WebApiOutputCacheAttribute.cs
示例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);
}
}
开发者ID:paulfryer,项目名称:NetworkListener,代码行数:32,代码来源:ServiceDescription.cs
示例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;
}
开发者ID:diouf,项目名称:apress-recipes-webapi,代码行数:16,代码来源:CacheAttribute.cs
示例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());
}
开发者ID:vanthoainguyen,项目名称:Flatwhite,代码行数:25,代码来源:TheMethodShouldIgnoreCache.cs
示例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);
}
开发者ID:minhkiller,项目名称:Flatwhite,代码行数:25,代码来源:CacheResponseBuilderTests.cs
示例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;
}
开发者ID:yyf919,项目名称:CacheCow,代码行数:8,代码来源:NotModifiedResponse.cs
示例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);
}
开发者ID:noahfalk,项目名称:corefx,代码行数:9,代码来源:CacheControlHeaderParserTest.cs
示例10: HttpCacheControlPolicyAttribute
/// <summary>
/// default .ctor is no cache policy
/// </summary>
public HttpCacheControlPolicyAttribute()
{
_cacheControl = new CacheControlHeaderValue()
{
Private = true,
NoCache = true,
NoStore = true
};
}
开发者ID:beyond-code-github,项目名称:CacheCow,代码行数:12,代码来源:HttpCacheControlPolicyAttribute.cs
示例11: SetClientCache
private CacheControlHeaderValue SetClientCache()
{
var cachecontrol = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(Duration),
MustRevalidate = true
};
return cachecontrol;
}
开发者ID:saibalghosh,项目名称:Layout3,代码行数:9,代码来源:CacheHttpGetAttribute.cs
示例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());
}
开发者ID:noahfalk,项目名称:corefx,代码行数:57,代码来源:CacheControlHeaderValueTest.cs
示例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;
}
开发者ID:c4rm4x,项目名称:C4rm4x.WebApi,代码行数:14,代码来源:ClientOnlyOutputCacheAttribute.cs
示例14: HttpCacheControlPolicyAttribute
public HttpCacheControlPolicyAttribute(bool isPrivate, int maxAgeInSeconds)
: this()
{
_cacheControl = new CacheControlHeaderValue()
{
Private = isPrivate,
Public = !isPrivate,
MustRevalidate = true,
MaxAge = TimeSpan.FromSeconds(maxAgeInSeconds)
};
}
开发者ID:sh54,项目名称:CacheCow,代码行数:11,代码来源:HttpCacheControlPolicyAttribute.cs
示例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());
}
开发者ID:minhkiller,项目名称:Flatwhite,代码行数:13,代码来源:CacheResponseBuilderTests.cs
示例16: HttpProvider_CustomCacheHeaderAndTimeout
public void HttpProvider_CustomCacheHeaderAndTimeout()
{
var timeout = TimeSpan.FromSeconds(200);
var cacheHeader = new CacheControlHeaderValue();
using (var defaultHttpProvider = new HttpProvider(null) { CacheControlHeader = cacheHeader, OverallTimeout = timeout })
{
Assert.IsFalse(defaultHttpProvider.httpClient.DefaultRequestHeaders.CacheControl.NoCache, "NoCache true.");
Assert.IsFalse(defaultHttpProvider.httpClient.DefaultRequestHeaders.CacheControl.NoStore, "NoStore true.");
Assert.AreEqual(timeout, defaultHttpProvider.httpClient.Timeout, "Unexpected default timeout set.");
Assert.IsNotNull(defaultHttpProvider.Serializer, "Serializer not initialized.");
Assert.IsInstanceOfType(defaultHttpProvider.Serializer, typeof(Serializer), "Unexpected serializer initialized.");
}
}
开发者ID:ChocolateMonkey,项目名称:onedrive-sdk-csharp,代码行数:14,代码来源:HttpProviderTests.cs
示例17: TryParse_SetOfValidValueStrings_ParsedCorrectly
public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
{
// Just verify parser is implemented correctly. Don't try to test syntax parsed by CacheControlHeaderValue.
CacheControlHeaderValue expected = new CacheControlHeaderValue();
expected.NoStore = true;
expected.MinFresh = new TimeSpan(0, 2, 3);
CheckValidParsedValue("X , , no-store, min-fresh=123", 1, expected, 29);
expected = new CacheControlHeaderValue();
expected.MaxStale = true;
expected.NoCache = true;
expected.NoCacheHeaders.Add("t");
CheckValidParsedValue("max-stale, no-cache=\"t\", ,,", 0, expected, 27);
}
开发者ID:noahfalk,项目名称:corefx,代码行数:14,代码来源:CacheControlHeaderParserTest.cs
示例18: Should_return_new_etag_if_cache_item_found_but_doesnt_match_checksum
public async Task Should_return_new_etag_if_cache_item_found_but_doesnt_match_checksum(string cacheChecksum, HttpStatusCode resultCode)
{
// Arrange
var cacheControl = new CacheControlHeaderValue
{
MaxStale = true,
MaxStaleLimit = TimeSpan.FromSeconds(15),
MinFresh = TimeSpan.FromSeconds(20)
};
var oldCacheItem = new WebApiCacheItem
{
CreatedTime = DateTime.UtcNow.AddSeconds(-11),
MaxAge = 10,
StaleWhileRevalidate = 5,
IgnoreRevalidationRequest = true,
ResponseCharSet = "UTF8",
ResponseMediaType = "text/json",
Content = new byte[0],
Key = "fw-0-HASHEDKEY",
Checksum = cacheChecksum
};
var request = new HttpRequestMessage
{
Method = new HttpMethod("GET"),
RequestUri = new Uri("http://localhost")
};
request.Headers.Add("If-None-Match", "\"fw-0-HASHEDKEY-OLDCHECKSUM\"");
var builder = new CacheResponseBuilder();
var handler = new EtagHeaderHandler(builder);
await Global.CacheStoreProvider.GetAsyncCacheStore().SetAsync("fw-0-HASHEDKEY", oldCacheItem, DateTimeOffset.Now.AddDays(1)).ConfigureAwait(false);
// Action
Global.Cache.PhoenixFireCage["fw-0-HASHEDKEY"] = new WebApiPhoenix(NSubstitute.Substitute.For<_IInvocation>(), new CacheInfo(), oldCacheItem, request);
var response = await handler.HandleAsync(cacheControl, request, CancellationToken.None).ConfigureAwait(false);
Assert.AreEqual(resultCode, response.StatusCode);
if (resultCode == HttpStatusCode.OK)
{
Assert.AreEqual($"\"fw-0-HASHEDKEY-{cacheChecksum}\"", response.Headers.ETag.Tag);
}
else
{
Assert.IsNull(response.Headers.ETag);
}
}
开发者ID:minhkiller,项目名称:Flatwhite,代码行数:50,代码来源:EtagHeaderHandlerTests.cs
示例19: Should_return_null_if_no_etag_in_request
public async Task Should_return_null_if_no_etag_in_request()
{
// Arrange
var cacheControl = new CacheControlHeaderValue();
var request = new HttpRequestMessage
{
Method = new HttpMethod("GET"),
RequestUri = new Uri("http://localhost")
};
var builder = Substitute.For<ICacheResponseBuilder>();
var handler = new EtagHeaderHandler(builder);
// Action
var response = await handler.HandleAsync(cacheControl, request, CancellationToken.None);
Assert.IsNull(response);
}
开发者ID:minhkiller,项目名称:Flatwhite,代码行数:17,代码来源:EtagHeaderHandlerTests.cs
示例20: Should_return_null_if_cache_item_not_found
public async Task Should_return_null_if_cache_item_not_found()
{
// Arrange
var cacheControl = new CacheControlHeaderValue();
var request = new HttpRequestMessage
{
Method = new HttpMethod("GET"),
RequestUri = new Uri("http://localhost")
};
request.Headers.Add("If-None-Match", "\"fw-1000-HASHEDKEY-CHECKSUM\"");
var builder = Substitute.For<ICacheResponseBuilder>();
var handler = new EtagHeaderHandler(builder);
// Action
var response = await handler.HandleAsync(cacheControl, request, CancellationToken.None);
Assert.IsNull(response);
}
开发者ID:minhkiller,项目名称:Flatwhite,代码行数:18,代码来源:EtagHeaderHandlerTests.cs
注:本文中的System.Net.Http.Headers.CacheControlHeaderValue类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论