ASP.NET MVC 자동 캐싱 옵션을 선택하는 방법은 무엇입니까?
asp.Net mvc 애플리케이션에서 자동 브라우저 캐싱을 중단하는 방법은 무엇입니까?
모든 링크를 캐시하기 때문에 캐싱에 문제가 있기 때문입니다. 그러나 여기서 캐싱을 저장하는 DEFAULT INDEX PAGE로 리디렉션 된 다음 해당 링크를 클릭 할 때마다 DEFAULT INDEX PAGE로 리디렉션됩니다.
그래서 어떤 사람은 ASP.NET MVC 4에서 캐싱 옵션을 수동으로 사용하는 방법을 있습니까?
을 사용하여 OutputCacheAttribute
컨트롤러의 특정 작업 또는 모든 작업에 대한 서버 및 / 또는 브라우저 캐싱을 제어 할 수 있습니다 .
컨트롤러의 모든 작업에 대해
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
// ...
}
특정 작업에 대하여 :
public class MyController : Controller
{
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
public ActionResult Index()
{
return View();
}
}
컨트롤러의 모든 모든 작업에 기본 캐싱 전략을 적용 하려면 메서드 를 편집 하고 찾아서 전역 작업 필터 를 추가 할 수 있습니다 . 이 메소드는 기본 MVC 응용 프로그램 프로젝트 템플릿에 추가됩니다.global.asax.cs
RegisterGlobalFilters
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new OutputCacheAttribute
{
VaryByParam = "*",
Duration = 0,
NoStore = true,
});
// the rest of your global filters here
}
이렇게하면 OutputCacheAttribute
모든 작업에 지정된 내용 이 적용되어 서버 및 브라우저 캐싱이됩니다. OutputCacheAttribute
특정 작업 및 컨트롤러 에 추가 하여이 캐시를 재정의 할 수 있어야합니다 .
HackedByChinese에 요점이 없습니다. 그는 서버 캐시를 클라이언트 캐시로 착각했습니다. OutputCacheAttribute는 브라우저 (클라이언트) 캐시가 아닌 서버 캐시 (IIS http.sys 캐시)를 제어합니다.
내 코드베이스의 아주 작은 부분을 제공합니다. 현명하게 사용하십시오.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class NoCacheAttribute : FilterAttribute, IResultFilter
{
public void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
var cache = filterContext.HttpContext.Response.Cache;
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetRevalidation(HttpCacheRevalidation.ProxyCaches);
cache.SetExpires(DateTime.Now.AddYears(-5));
cache.AppendCacheExtension("private");
cache.AppendCacheExtension("no-cache=Set-Cookie");
cache.SetProxyMaxAge(TimeSpan.Zero);
}
}
용법 :
/// will be applied to all actions in MyController
[NoCache]
public class MyController : Controller
{
// ...
}
실제로 모든 클라이언트 캐시를 비활성화하므로 현명하게 사용하십시오. 비활성화되지 않은 유일한 캐시는 "뒤로 단추"브라우저 캐시입니다. 그러나 그것을 우회 할 방법이 정말로없는 것 같습니다. 자바 스크립트를 사용하여 감지하고 페이지 또는 페이지 영역을 강제로 새로 고쳐야 할 수도 있습니다.
중복 코드를 방지하기 위해 페이지에서 캐시 값을 개별적으로 설정하는 대신 Web.config 파일에서 캐시 프로필을 설정할 수 있습니다. OutputCache 특성의 CacheProfile 속성을 사용하여 프로필을 참조 할 수 있습니다. 이 캐시 프로필은 페이지 / 메서드가 이러한 설정을 재정의하지 않는 한 모든 페이지에 적용됩니다.
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheProfile" duration="60" varyByParam="*" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
특정 작업 또는 컨트롤러에서 캐싱을 비활성화하려면 아래와 같이 특정 작업 방법을 장식하여 구성 캐시 설정을 재정의 할 수 있습니다.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult NoCachingRequired()
{
return PartialView("abcd");
}
이것이 명확하고 유용하기를 바랍니다.
브라우저 캐싱을 방지하려면 ShareFunction에서이 코드를 사용할 수 있습니다.
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);
}
페이지 솔루션의 경우 레이아웃 페이지에서 설정하십시오.
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
내 답변을 모든 사람에게 표시하기 위해이 질문에 대한 답변으로 댓글을 이동합니다.
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
이것은 모든 브라우저 (IE, Firefox 및 Chrome도 마찬가지)에서 작동합니다. 내 대답이 @Joseph Katzman에게 효과가 있다는 소식을 듣고 기쁩니다.
참고 URL : https://stackoverflow.com/questions/12948156/asp-net-mvc-how-to-disable-automatic-caching-option
'ProgramingTip' 카테고리의 다른 글
자바 펼쳐는 해시 값으로 페이지를 다시로드합니다. (0) | 2020.10.28 |
---|---|
크롬 자바 펼쳐 디버거 중단 점은 아무것도하지 않나요? (0) | 2020.10.28 |
wkhtmltopdf의 테이블 행 내부에서 페이지 나누기를 방지하는 방법 (0) | 2020.10.28 |
db : test : clone, db : test : clone_structure, db : test : load 및 db : test : prepare의 차이점은 무엇입니까? (0) | 2020.10.27 |
iPhone / iPad / iPod touch의 색상 감지? (0) | 2020.10.27 |