我想在加载网页时在浏览器上将图像,css和javascript缓存在客户端.有很多不同类型的缓存我很困惑,哪些与asp.net mvc一起使用.
是否也可以让他们的浏览器检查这些文件的新版本或修改版本?
谢谢!
我有一个控制器动作接收一个复杂的对象作为参数,我需要OutputCache来改变这个复杂对象的属性之一.这可能吗?怎么样?
我对ASP.NET MVC缓存和授权感到困惑,并且急需一些澄清.
我的自制授权属性继承自AuthorizeAttribute.它的重写AuthorizeCore方法每次都会运行,即使我[OutputCache]在控制器操作上设置了一个属性.我明白那一部分.
现在,对我来说这个问题:现在当我实际执行输出缓存并且从缓存中提供页面时,每次AuthorizeCore都会失败.原因是当缓存请求时,httpContext.Session提供的AuthorizeCore是null!?这是一些简化的代码:
protected override bool AuthorizeCore(HttpContextBase httpContext) {
return (Session["userId"] != null)
}
Run Code Online (Sandbox Code Playgroud)
所以如果httpContext.Session是null,这显然每次都失败了.我需要访问会话,我还能如何检查请求是否被授权?这没有任何意义 - 如果这是它应该如何,那么我永远不能在ASP.NET MVC中使用缓存页面和身份验证.救命?
ResponseCache有点替代OutputCache; 但是,我想做服务器端缓存以及每个参数输入.
根据这里和这里的一些答案,我应该使用IMemoryCache或者IDistributedCache这样做.我特别感兴趣的是在参数不同的控制器上进行缓存,之前在asp.net 4中使用过OutputCache,VaryByParam如下所示:
[OutputCache(CacheProfile = "Medium", VaryByParam = "id", Location = OutputCacheLocation.Server)]
public ActionResult Index(long id)
{
///...
}
Run Code Online (Sandbox Code Playgroud)
我将如何在asp.net核心中复制这个?
大约每月一次,我们遇到了一个奇怪的错误,我没有解释.错误是这样的:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List`1.Enumerator.MoveNext() at
System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
Run Code Online (Sandbox Code Playgroud)
这是发生错误的代码.在MyObject的构造函数中调用此方法:
// pull a list of MyObjects from the cache so we can see if this one exists
List<MyObject> MyObjectList= System.Web.HttpRuntime.Cache["MyObjects"] as List<MyObject>;
if (MyObjectList!= null)
{
// this is where the error happens. Just getting an object out based on its id
MyObject obj = MyObjectList.FirstOrDefault(m => m.Id == this.Id);
if(obj != null){
// great, it already exists in the cache
} …Run Code Online (Sandbox Code Playgroud)