ASP. net mvc action filter-caching and Compression

Source: Internet
Author: User

Caching plays a major role in developing highly scalable Web applications. we can cache any http get request in the user browser for a predefined time, if the user request the same URL in that predefined time the response will be loaded from the browser cache instead of the server. you can archive the same in ASP. net MVC application with the following action filter:

 Using System;
Using System. Web;
Using System. Web. MVC;

Public Class Cachefilterattribute : Actionfilterattribute
{
/// <Summary>
/// Gets or sets the cache duration in seconds. The default is 10 seconds.
/// </Summary>
/// <Value> The cache duration in seconds. </Value>
Public Int Duration
{
Get ;
Set ;
}

Public Cachefilterattribute ()
{
Duration = 10;
}

Public Override Void Onactionexecuted ( Filterexecutedcontext Filtercontext)
{
If (Duration <= 0) Return ;

Httpcachepolicybase Cache = filtercontext. httpcontext. response. cache;
Timespan Cacheduration = Timespan . Fromseconds (duration );

Cache. setcacheability ( Httpcacheability . Public );
Cache. setexpires ( Datetime . Now. Add (cacheduration ));
Cache. setmaxage (cacheduration );
Cache. appendcacheextension ( "Must-revalidate, proxy-revalidate" );
}
}

You can apply the filter in your controller action method like the following.

 
[Cachefilter(Duration = 60)]
Public VoidCategory (StringName,Int? Page)

The following shows the screen-shot in firebug when cache FilterIs not applied:

And this is the screen-shot when the cache FilterIs applied:

Another important thing is compression. now a days, all modern browsers accept compressed contents and it saves huge bandwidth. you can apply the following action filter to compress your response in your asp. net MVC application:

 Using System. Web;
Using System. Web. MVC;

Public Class Compressfilter : Actionfilterattribute
{
Public Override Void Onactionexecuting ( Filterexecutingcontext Filtercontext)
{
Httprequestbase Request = filtercontext. httpcontext. request;

String Acceptencoding = request. headers [ "Accept-encoding" ];

If ( String . Isnullorempty (acceptencoding )) Return ;

Acceptencoding = acceptencoding. toupperinvariant ();

Httpresponsebase Response = filtercontext. httpcontext. response;

If (Acceptencoding. Contains ( "Gzip" ))
{
Response. appendheader ( "Content-encoding" , "Gzip" );
Response. Filter = New Gzipstream (Response. filter, Compressionmode . Compress );
}
Else If (Acceptencoding. Contains ( "Deflate" ))
{
Response. appendheader ( "Content-encoding" , "Deflate" );
Response. Filter = New Deflatestream (Response. filter, Compressionmode . Compress );
}
}
}

Just decorate your controller action with this filter:

 
[Compressfilter]
Public VoidCategory (StringName,Int? Page)

The following shows when compressionIs not applied:

And this is the screen-shot when the compress FilterIs applied:

You can also apply both these filter in the same action method, like the following:

 
[Compressfilter(Order = 1)]
[Cachefilter(Duration = 60, order = 2)]
Public VoidCategory (StringName,Int? Page)

And this is the screen-shot:

Enjoy !!! (From: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx)

Related Article

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.