csharp 没有MVC的缓存设置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 没有MVC的缓存设置相关的知识,希望对你有一定的参考价值。
// for whole site place in global.asax
// http://stackoverflow.com/questions/7087859/how-do-i-add-site-wide-no-cache-headers-to-an-mvc-3-app
void Application_PreSendRequestHeaders(Object sender, EventArgs e)
{
// min
Response.Cache.SetCacheability(HttpCacheability.Private);
// full
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
// Attribute for controler
// http://stackoverflow.com/questions/10011780/prevent-caching-in-asp-net-mvc-for-specific-actions-using-an-attribute
// To ensure that JQuery isn't caching the results, on your ajax methods, put the following:
$.ajax({
cache: false
//rest of your ajax setup
});
// Or to prevent caching in MVC, we created our own attribute, you could do the same. Here's our code:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.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);
}
}
// Then just decorate your controller with [NoCache]. OR to do it for all you could just put the attribute on the class of the base class that you inherit your controllers from (if you // have one) like we have here:
[NoCache]
public class ControllerBase : Controller, IControllerBase
// You can also decorate some of the actions with this attribute if you need them to be non-cacheable, instead of decorating the whole controller.
// If your class or action didn't have NoCache when it was rendered in your browser and you want to check it's working, remember that after compiling the changes you need to do a "hard // refresh" (Ctrl+F5) in your browser. Until you do so, your browser will keep the old cached version, and won't refresh it with a "normal refresh" (F5).
// **** Useful Cache-Control response headers include:
// •max-age=[seconds] — specifies the maximum amount of time that a representation will be considered fresh. Similar to Expires, this directive is relative to the time of the request,
// rather than absolute. [seconds] is the number of seconds from the time of the request you wish the representation to be fresh for.
// •s-maxage=[seconds] — similar to max-age, except that it only applies to shared (e.g., proxy) caches.
// •public — marks authenticated responses as cacheable; normally, if HTTP authentication is required, responses are automatically private.
// •private — allows caches that are specific to one user (e.g., in a browser) to store the response; shared caches (e.g., in a proxy) may not.
// •no-cache — forces caches to submit the request to the origin server for validation before releasing a cached copy, every time. This is useful to assure that authentication is
// respected (in combination with public), or to maintain rigid freshness, without sacrificing all of the benefits of caching.
// •no-store — instructs caches not to keep a copy of the representation under any conditions.
// •must-revalidate — tells caches that they must obey any freshness information you give them about a representation. HTTP allows caches to serve stale representations under special
// conditions; by specifying this header, you’re telling the cache that you want it to strictly follow your rules.
// •proxy-revalidate — similar to must-revalidate, except that it only applies to proxy caches.
以上是关于csharp 没有MVC的缓存设置的主要内容,如果未能解决你的问题,请参考以下文章
csharp 用于ASP.NET MVC的Castle Windsor IoC容器设置