开发者

how Asp.net MVC partialview can be cached on the base of parameter

开发者 https://www.devze.com 2023-02-26 03:25 出处:网络
public PartialViewRes开发者_如何学Cult res(int days) I want to cache this partialview on the base of parameter pass to this result.if parameter goes different then cache are seprate for every diffe
 public PartialViewRes开发者_如何学Cult res(int days)

I want to cache this partialview on the base of parameter pass to this result.if parameter goes different then cache are seprate for every different parameter passed to action.

are i can destroy this cache before time period. can a access this cache made in it.


It is not really clear what you really want. I thought about 2 applications.

    public class OutputCacheExt : OutputCacheAttribute
    {
        public string DaysFromParam { get; set; }
        public int Days
        {
            get { return TimeSpan.FromSeconds(Duration).Days; }
            set { Duration = (int)TimeSpan.FromDays(value).TotalSeconds; }
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!string.IsNullOrEmpty(DaysFromParam))
            {
                int days = 0;
                int.TryParse(filterContext.HttpContext.Request[DaysFromParam], out days);
                Days = days;
            }
            base.OnActionExecuting(filterContext);
        }
    }

First application: If you want just set cache in days:

[OutputCacheExt(VaryByParam="none", Days=5)]
public PartialViewResult res(int days){}

Second application: If you want set days from http param:

[OutputCacheExt(VaryByParam="none", DaysFromParam="days")]
public PartialViewResult res(int days){}


I hope I have understood your question. In order for you to do caching in asp.net mvc you use the output cache attribute as follows:

[OutputCache(Duration=10, VaryByParam="none")] public PartialViewResult res(int days) In this case the Duration is 10 seconds.

Hope it answers your question

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号