【问题标题】:outputcache not working when the action called by other action当其他操作调用的操作时,输出缓存不起作用
【发布时间】:2015-06-26 08:35:52
【问题描述】:

ASP.NET MVC4!

代码:

    [OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
    public ActionResult Index(int id = 0)
    {
        return Content(DateTime.Now.ToString());
    }

上面的代码运行良好。但代码:

    public ActionResult Index(int id = 0)
    {
        ActionResult result = Test(id);

        return result;
    }

    [OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
    ActionResult Test(int id)
    {
        return Content(DateTime.Now.ToString());
    }

不工作!

我也这样尝试:

    public ActionResult Index(int id = 0)
    {
        return Test(id);
    }

每个请求也不会输出相同的值!

【问题讨论】:

    标签: asp.net-mvc-4 outputcache


    【解决方案1】:

    你应该使用

    return RedirectToAction("Index", "Home", new { id = 1 });
    

    像这样。

    public ActionResult Index(int id = 0)
        {
             return RedirectToAction("Test", "Home", new { id = 1 });
        }
    
        [OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
        ActionResult Test(int id)
        {
            return Content(DateTime.Now.ToString());
        }
    

    【讨论】:

    • 但是当请求来自 android 或 iphone 等应用程序时我能做什么?这种方式不会得到正确的结果,因为这不是 Response.write() 的方式,而是设置响应头位置值到主机名/home/test/id!顺便说一句,我用的是 ApiController!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多