. NET MVC disables browser caching

Source: Internet
Author: User

I'm looking for ways to disable browser caching for the entire ASP. NET MVC Site

I found the following methods,

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Also meta tag method (it won't work for me because some MVC operations send part Html/json via Ajax, headless, meta tags)

<meta. http-equiv="PRAGMA" content="NO-CACHE">

But I'm looking for a simple way to disable browser caching for the entire site.

Workaround 1:
HttpContext.Current.Response.Cache.SetExpires(Datetime.UtcNow.AddDays(-1))
HttpContext.Current.Response.Cache.SetValidUntilExpires(False)
HttpContext.Current.Response.Cache.Setrevalidation (httpcacherevalidation. Allcaches
httpcontext. Current. Response. Cache. Setcacheability (httpcacheability. Nocache
httpcontext. Current. Response. Cache. Setnostore ()

All requests get routed through Default.aspx for the first time-so assume you can just pop out there in the code later.

Workaround 2:

Create a class that inherits from Iactionfilter.

Public Class NoCache : ActionFilterAttribute
{
Public Override void Onresultexecuting(ResultExecutingContextFiltercontext)
{
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. Onresultexecutingfiltercontext     }
}

Then put the attributes where needed ...

[NoCache]
[HandleError]
Public Class AccountController : controller
{< Span class= "PLN" >
    [nocache
    [authorize
    public actionresult< Span class= "PLN" > changepassword ()
    {
        return view ();     }
}

. NET MVC disables browser caching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.