我正在使用OutputCache属性在服务器端缓存我的动作的html输出.
很好,它可以工作,但现在我的情况是内容很少变化,但是当它发生时,用户在下一个请求中看到新数据至关重要.
那么,有没有办法以编程方式中止页面缓存持续时间?
我正在调查MVC3源并遇到以下(在OutputCacheAttribute.cs中),在生成用于输出缓存的键时调用它:
// The key is typically too long to be useful, so we use a cryptographic hash
// as the actual key (better randomization and key distribution, so small vary
// values will generate dramtically different keys).
using (SHA256 sha = SHA256.Create()) {
return Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(uniqueIdBuilder.ToString())));
}
Run Code Online (Sandbox Code Playgroud)
评论说需要使用哈希,因为"密钥通常太长而无法使用".任何人都可以阐明这一点,并建议在asp.net中缓存键的最大长度?
当控制器操作返回 RedirectResult 结果时,输出缓存过滤器似乎不适用。
以下是如何重现 ASP.Net MVC3 默认 Internet Web 应用程序的问题:
在 Web.config 中:
<system.web>
<caching>
<outputCache enableOutputCache="true"></outputCache>
<outputCacheSettings>
<outputCacheProfiles>
<add name="ShortTime" enabled="true" duration="300" noStore="false" />
</outputCacheProfiles>
</outputCacheSettings>
</caching> ...
Run Code Online (Sandbox Code Playgroud)
在 HomeController.cs 中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcOutputCacheRedir.Controllers
{
public class HomeController : Controller
{
[OutputCache(CacheProfile = "ShortTime")]
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
[OutputCache(CacheProfile = "ShortTime")]
public ActionResult About()
{
// Output cache works as expected …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个MVC 3应用程序,最近转换为mvc 4并添加了移动视图.我想为iPad专门为平板电脑添加桌面视图的移动显示模式(主要来自此处的流量).
我在我的代码中就是这样的
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode()
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
});
DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("Mobile", StringComparison.OrdinalIgnoreCase) >= 0)
});
Run Code Online (Sandbox Code Playgroud)
并设置输出缓存changebycustom如下所示
public override string GetVaryByCustomString(HttpContext context, string custom)
{
string strUserAgent = context.Request.UserAgent.ToLower();
if (strUserAgent.Contains("ipad"))
{
return base.GetVaryByCustomString(context, custom);
}
if (Request.Browser.IsMobileDevice)
{
return "mobile";
}
return base.GetVaryByCustomString(context, custom);
}
Run Code Online (Sandbox Code Playgroud)
我在移动设备和桌面设备上都使用相同的网址.
问题: 问题是在将应用程序部署到azure之后.1小时后,移动设备可以获取几个网址的桌面视图.存在不一致.
任何人都可以帮助我,我错了.我甚至关闭了 outputcache仍然是同样的问题.
如果我将以下内容添加到我的应用程序/控制器
[OutputCache(Duration = 7200)]
Run Code Online (Sandbox Code Playgroud)
我在网站上有两个用户,User1和User2.如果User1浏览到打开了上述OutputCache的页面,则服务器会在页面中缓存HTML.
如果User2然后请求相同的页面,服务器是否必须重新执行该页面(因为它是一个不同的用户),或者它是否使用User1访问页面时相同的缓存副本?
这就是我所拥有的:
[OutputCache(Duration = 3600, VaryByParam = "model")]
public object Hrs(ReportFilterModel model) {
var result = GetFromDatabase(model);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我希望它为每个不同的模型缓存一个新结果.目前它正在缓存第一个结果,即使模型发生变化,它也会返回相同的结果.
我甚至试图覆盖ReportFilterModel的方法ToString和GetHashCode方法.实际上我有更多的属性我想用来生成独特的HashCode或String.
public override string ToString() {
return SiteId.ToString();
}
public override int GetHashCode() {
return SiteId;
}
Run Code Online (Sandbox Code Playgroud)
任何建议,我如何使用复杂的对象OutputCache?
public class ValuesController : ApiController
{
[System.Web.Mvc.OutputCache(Duration = 3600)]
public int Get(int id)
{
return new Random().Next();
}
}
Run Code Online (Sandbox Code Playgroud)
由于缓存设置为1小时,我希望Web服务器为每个具有相同输入的请求保持返回相同的数字,而不再执行该方法.但事实并非如此,缓存属性没有效果.我做错了什么?
我使用MVC5,我从VS2015和IIS Express进行了测试.
问题:我有一个ASP.NET网站,我不相信我的代码正确地获得OutputCached.我正在使用IIS7性能计数器向我显示点击或错过一秒钟.
我有一个简单的ASP.NET MVC网站.我正在使用内置的ASP.NET输出缓存魔术.
这是一些示例代码: -
[AcceptVerbs(HttpVerbs.Get)]
[ApiAuthorize] // <-- this checks the querystring for a "key=1234".
// Doesn't find it, then it throws a 401 NOT AUTH exception.
[OutputCache(CacheProfile = "HomeController_Foo")]
public ActionResult Foo(string name, byte? alpha, byte? beta)
{
}
Run Code Online (Sandbox Code Playgroud)
所以这意味着每个网址查询可以如下所示: -
现在,请注意我是如何让OutputCache引用配置文件的?这里是...
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="HomeController_Foo" duration="3600" varyByParam="key;name;alpha;beta"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Run Code Online (Sandbox Code Playgroud)
没什么太难的......
所以这里是踢球者!当我通过使用IIS7性能计数器确认发生这种情况时,它表示输出缓存未命中/秒是我正在进行的请求的100%.输出缓存命中数为0 /秒.
我正在使用第三方Web负载压力测试程序来查询我的网站.现在,源数据是什么?名单.程序循环遍历所有名称,然后返回到开始,冲洗重复.因此,至少调用一次相同的查询字符串是有限的.IIS日志文件确认了这一点.
我没有传递alpha或beta的任何数据.
这是我的查询字符串我正在打....
...我继续使用数据源文件中的名称替换'hello + world',并且IIS日志确认了这一点.
那么..我在看错了性能指标吗?有没有其他技巧可以看出它是否正在获得输出缓存?代码非常快,因此很难判断这是否是缓存结果.
我有一个ASP.NET MVC 3应用程序,其动作同时使用RequireHttps和OutputCache属性:
[RequireHttps]
[OutputCache(Duration = 14400, VaryByCustom = "CurrentUser"]
public ActionResult VersionB()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
当我导航到该页面时,我会按预期重定向到HTTPS.
但是,在初始页面加载之后,我仍然可以通过HTTP访问页面.如果我删除该OutputCache属性,我将无法再通过HTTP访问该页面.
似乎OutputCache忽略了HTTPS,因此允许对页面进行不安全的访问.是否可以缓存通过HTTPS提供的操作?
我知道我可以在web.config文件中设置OutputCacheProfiles.
我想知道如何将不同的缓存配置文件应用于页面(控制器)级别上的不同用户角色?
outputcache web-config browser-cache user-roles asp.net-mvc-3
outputcache ×10
asp.net-mvc ×7
caching ×4
asp.net ×3
c# ×3
mobile ×1
performance ×1
requirehttps ×1
user-roles ×1
viewengine ×1
web-config ×1