Output Caching for Css Files in ASP.NET MVC

by Administrator 31. August 2011 05:13

ASP.NET MVC offers an easy way to implement output caching using an attribute:


[OutputCache( VaryByParam = "pathInfo", Duration = 60 )]
public virtual ActionResult Index( string pathInfo )

What this does is storing an in memory version of the generated output for 60 minutes for the given path info. MVC handles such attributes in a pipeline where each attribute has a defined sort order. Then there is a chance to execute before and after the next action in the pipeline. Unfortunately output caching does not the preserve the content type set. It always emits text/html which breaks when you try to emit something else (in my case CSS).

I found this post on Stack Overflow that pointed me into the right direction. It just defines a content encoding attribute that should run late in the papeline. It is even nice to use this attribute when not running into the caching issue since you can decorate your controller action with the content type it returns. Here is the attribute:


public class ContentTypeAttribute : ActionFilterAttribute
{
  protected const int EXECUTION_ORDER = 1000;

  public string ContentType
  {
     get;
     set;
  }

  public ContentTypeAttribute ()
  {
     Order = EXECUTION_ORDER;
  }

  public override void OnResultExecuted( ResultExecutedContext filterContext )
  {
     if( string.IsNullOrEmpty( ContentType ) )
     {
        throw new InvalidOperationException( "The attribute requires a content type." );
     }

     filterContext.HttpContext.Response.ContentType = ContentType;
  }
}

To use this attribute mark the controller action as following:


[ContentType( ContentType = "text/css" )]
[OutputCache( VaryByParam = "pathInfo", Duration = 60 )]

The only thing I relly changed from the proposed solution is hardcoding the sort order into the attribute since I do not see any reason to make the implementer specify it.

Tags: ,

ASP.NET MVC

About Online Gaming

by Administrator 23. August 2011 03:54

Check out this comic. Hillarious.

Tags:

Fun Stuff

The Magic of Truth and Lies

by Administrator 13. August 2011 05:25

Check out this video with Marco Tempest on TED. Great entertainment.

Tags: ,

About the author

for comments and suggestions contact:

 

Month List