通过querystring参数清除outputcache [英] Clear outputcache via querystring parameter

查看:108
本文介绍了通过querystring参数清除outputcache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一些MVC3控制器操作上使用OutputCache属性。当用户传递参数"live = true"时我想从服务器缓存中删除该缓存的请求。有没有办法用OutputCache做到这一点?我尝试了以下内容:

I want to use the OutputCache attribute on some of my MVC3 controller actions. When a user passes a parameter "live=true" I want to remove that cached request from the server cache. Is there a way to do this with OutputCache? I tried the following:

public class LiveOutputCacheAttribute : OutputCacheAttribute
{
    private const string _resetParam = "live";
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var context = filterContext.HttpContext;
        AddLiveToVaryByParam();
        if (context.Request[_resetParam] == "true")
        {
            var urlToRemove = GetUrlToRemove(filterContext);
            context.Response.RemoveOutputCacheItem(urlToRemove);
            return;
        }
        base.OnActionExecuting(filterContext);
    }

    private void AddLiveToVaryByParam()
    {
        // add live reset flag when vary by param is specified
        if (VaryByParam != "*" && !VaryByParam.Contains("live"))
            VaryByParam = string.Format("{0};{1}",VaryByParam, _resetParam).TrimStart(';');
    }

    private static string GetUrlToRemove(ActionExecutingContext filterContext)
    {
        var routeValues = new RouteValueDictionary(filterContext.ActionParameters);
        var urlHelper = new UrlHelper(filterContext.RequestContext);
        string action = filterContext.ActionDescriptor.ActionName;
        string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
        return urlHelper.Action(action, controller, routeValues);
    }
}

这就是我在行动中使用此属性的方式:

This is how I use this attribute on my action:

[LiveOutputCache(Location = OutputCacheLocation.Server, Duration = 60 * 60, VaryByParam = "name")]
public ActionResult Index(string name)
{
    ViewData.Model = name + "-----" +  DateTime.Now.Ticks.ToString();
    return View();
}

然而,这种方法似乎没有清除缓存当我使用live = true属性时。相反,它只是缓存该请求。 

However, this approach does not seem to be clearing the cache when I use the live=true attribute. Instead it just caches that request as well. 

推荐答案

您好Vadim,

Hi Vadim,

对于此类与MVC相关的问题,请在此处尝试:  http://forums.asp.net/1146.aspx/1?MVC  

For such MVC related issues, please try here: http://forums.asp.net/1146.aspx/1?MVC 

谢谢。

祝你好运,


这篇关于通过querystring参数清除outputcache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆