当前位置: 首页>>代码示例>>C#>>正文


C# Headers.CacheControlHeaderValue类代码示例

本文整理汇总了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;
 }
开发者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


注:本文中的System.Net.Http.Headers.CacheControlHeaderValue类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。