c# - asp.net mvc 3 WebCache 奇怪的行为

标签 c# asp.net asp.net-mvc-3

我正在寻找(如果可能的话)详细解释为什么会发生以下事情:

我正在使用 asp.net 的 WebCache 静态类来存储我的对象

   public ActionResult Index(int page = 0)
    {

        const int count = 5;

        var recordsToSkip = page*count;
        if (WebCache.Get("page-" + page + "-5") == null)
        {
            var records =
                db.Submissions.OrderByDescending(x => x.SubmitedOn).Skip(recordsToSkip).Take(count).ToList();
            var index = new IndexViewModel()
                            {
                                ChallengeEntries = records,
                                CurrentPage = 0
                            };
            WebCache.Set("page-" + page + "-5", index);
        }

        return View(WebCache.Get("page-" + page + "-5"));
    }

我的观点是:

<div class="pagination">
        @Model.CurrentPage  // here just to let me watch the value of the current page
        @if(Model.CurrentPage >=1)
        {


            @Html.ActionLink("Previous", "Index", new { page = Model.CurrentPage-- })
        }

        @if(Model.ChallengeEntries.Count()>=5)
        {

            @Html.ActionLink("Next", "Index", new { page = Model.CurrentPage++ })
        }

    </div> 

所以我想知道为什么 Model.CurrentPage++ 增加当前缓存对象的值并保存它,而不是只返回值+1而不修改缓存?

最佳答案

当您从缓存中访问时,您正在访问缓存中对象的引用。

这就是为什么当增加它的值时,它会被更新并保存在缓存中。

关于c# - asp.net mvc 3 WebCache 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934165/

相关文章:

c# - .net core类库dll与文件夹名称相同

c# - Linq 代码无法正常工作

javascript - 确认或取消从我的 Excel 文件上传数据后

c# - View 中的多个模型 ASP.NET MVC(部分 View 方法)

c# - 对 ViewModel 属性进行修饰以使用不同的名称进行绑定(bind)

html - 使用 anchor 标记调用 Action

c# - 为什么这个 Linq 不工作

c# - ASPNET MVC - 为什么 ModelState.IsValid false "The x field is required"当该字段确实有值时?

c# - Dispose() 如何知道它是因为异常而被调用的?

javascript - 将 javascript 事件绑定(bind)到 MVC 控件