Thursday, 17 October 2013

No Cache Attribute in MVC

    using System;
using System.Web;
using System.Web.Mvc;
using RRA.Core.Services.Utilities;

///
/// Disables browser caching.
///

[AttributeUsage(AttributeTargets.Method)]
public class NoCacheAttribute : ActionFilterAttribute
{
///
/// Called by the ASP.NET MVC framework before the action result executes.
///

/// The filter context.
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetETag((Guid.NewGuid()).ToString());
filterContext.HttpContext.Response.Cache.SetExpires(SystemDateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}


No comments:

Post a Comment

How to find the last interactive logons in Windows using PowerShell

Use the following powershell script to find the last users to login to a box since a given date, in this case the 21st April 2022 at 12pm un...