asp.net – 当请求具有查询字符串时,Web服务的缓存不起作用
发布时间:2020-12-16 09:37:31 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试实现Web服务调用的客户端缓存,并且基于来自Web的信息,我能够根据SetCachingPolicy()函数执行它,如下面的代码1所示. 我能够成功地使用Web方法RetrieveX,但不能使用方法RetrieveY.我注意到RetrieveX没有参数,RetrieveY有一个字符串参数,在Fiddler下
我正在尝试实现Web服务调用的客户端缓存,并且基于来自Web的信息,我能够根据SetCachingPolicy()函数执行它,如下面的代码1所示.
我能够成功地使用Web方法RetrieveX,但不能使用方法RetrieveY.我注意到RetrieveX没有参数,RetrieveY有一个字符串参数,在Fiddler下检查时,差异似乎是来自RetrieveY的Web服务调用的HTTP GET请求有一个参数的查询字符串. 到目前为止,没有查询字符串的所有HTTP GET Web服务调用都正确地执行了缓存,但是此调用中没有查询字符串. Fiddler下的检查表明RetrieveX在输出1中具有以下缓存信息,而RetrieveY在输出2中具有信息. 这是否是这种缓存方法的限制,还是我可以做一些事情来让RetrieveY的客户端缓存工作? 代码1:SetCachingPolicy private void SetCachingPolicy() { HttpCachePolicy cache = HttpContext.Current.Response.Cache; cache.SetCacheability(HttpCacheability.Private); cache.SetExpires(DateTime.Now.AddSeconds((double)30)); FieldInfo maxAgeField = cache.GetType().GetField( "_maxAge",BindingFlags.Instance | BindingFlags.NonPublic); maxAgeField.SetValue(cache,new TimeSpan(0,30)); } 代码2:RetrieveX [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] public string[] RetrieveX() { SetCachingPolicy(); // Implementation details here. return array; } 代码3:检索 [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] public string[] RetrieveY(string arg1) { SetCachingPolicy(); // Implementation details here. return array; } 输出1:RetrieveX缓存信息 HTTP/200 responses are cacheable by default,unless Expires,Pragma,or Cache-Control headers are present and forbid caching. HTTP/1.0 Expires Header is present: Wed,12 Sep 2012 03:16:50 GMT HTTP/1.1 Cache-Control Header is present: private,max-age=30 private: This response MUST NOT be cached by a shared cache. max-age: This resource will expire in .5 minutes. [30 sec] 输出2:检索缓存信息 HTTP/200 responses are cacheable by default,or Cache-Control headers are present and forbid caching. HTTP/1.0 Expires Header is present: -1 Legacy Pragma Header is present: no-cache HTTP/1.1 Cache-Control Header is present: no-cache no-cache: This response MUST NOT be reused without successful revalidation with the origin server. 解决方法
我也遇到过这个问题,我想我会分享对我有用的东西.根本问题是没有在响应上设置VaryByParams.如果将其添加到SetCachingPolicy()方法中,RetrieveY应该按预期开始工作:
cache.VaryByParams["*"] = true (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何指定相对于站点根目录的路径?
- 新单词 part 15
- asp.net:如何在button onclick上使用eventargs进行参数传递
- ASP.NET MVC中多个浏览器选项卡中的唯一会话
- asp.net-mvc-4 – WepApi控制器是否应该返回viewmodels
- asp.net – 迁移匿名配置文件的最佳方式
- ASP.NET Identity Manager错误:尝试创建“MetaController”
- 具有自定义角色的ASP.NET MVC和Windows身份验证
- asp.net – web.config转换未在VS2010中显示
- asp-classic – CDO电子邮件主题特殊字符错误
推荐文章
站长推荐
- asp.net-mvc – FluentValidation客户端验证
- Asp.net 实现Session分布式储存(Redis,Mongodb,M
- asp.net – 在VS Code中指定localhost端口的位置
- asp.net – Visual Studio 2005:是否有一个简单
- asp.net – 使用DataPager而不检索每个请求的所有
- ASP.NET Web应用程序死锁 – 认为它是由SQL Serv
- asp.net-mvc – 在ASP.NET MVC组织帐户中访问Azu
- .net – Ninject的InRequestScope发生了什么事
- 获取所选的输入类型单选按钮ASP.NET
- asp.net – 使用Web.Config设置我的SQL数据库连接
热点阅读