March 2008 - Posts

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 void Category(string name, int? page)

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

and this is the screen-shot when the cache filter is 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 void Category(string name, int? page)

The following shows when compression is not applied:

and this is the screen-shot when the compress filter is 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 void Category(string name, int? page)

And this is the screen-shot:


Enjoy!!!

Download: Source.zip

kick it on DotNetKicks.com

[Note: This post was meant to be published the day after MIX08 version was out]

I have upgraded Kigg with the new bits.  Upgrading was plain and simple, though I created a new project and added the files instead of changing the Guids. There has been few changes in the web project which are required by the MIX08 release such as routing declaration, removing attribute for controller actions, using lambdas for action link where it is appropriate and finally changing the Unit test with Scott Hanselman's MVCMockHelper and Fake View Engine from Phil Hack's Test Specific Subclass pattern.

As Phil Hack already mention the  MIX08 release was focused on to improve the Routing system and the next drop will streamline the testability issues, I think it is the right time to put my requirement list and comments on the new bits, not in any specific order, here I go:

Mix Release:

  • It is good to see that controller now can implement IDisposable, I was wondering why  not going one step further and make the IController inherits from IDisposable?
  • The ControllerAction has vanished, certainly a good choice and I am sure the in next .NET Framework, we do not have to deal with WebMethod and OperationContract attributes anymore, of course it does not make any sense having a double standard in the same platform.
  • Filter attributes is a better substitute of HttpModule in MVC world.
  • The team has not published any list on the Bug that has been fixed in this release. But I am sure there has not been many, currently two things that are really bothering me:
    • It is still not possible to use a base class in Master Page and a concrete class in Content Page. (Details)
    • You cannot turn-off the Session Module. (Details)
  • Previous MVCToolkit has been imported into the core MVC framework which is good, but I would like to see most of the Url building methods of  HTMLHelper are also available in UrlHelper, specially those with lambda versions.

Future Release:

  • Validation Support. Currently we have to write both client side and server side codes, which is really painful, we need to have a similar kind of web form validation controls in MVC space.
  • Close Integration with ASP.NET AJAX. I think the main reason that ASP.NET Ajax is mostly used framework in developing ASP.NET application due to its tight integration with ASP.NET. We need the same kind of support in MVC. Currently, the AjaxHelper is left empty, I guess later on the partial rendering will be implemented over there, but I would like to put more emphasis on the controller actions, what I would love to see is some attributes e.g. AjaxOperation, AjaxMethod which will generate the client side proxy, same as we have today for the web service and page methods.
  • I do not believe that those Control Helper methods of Rob Conery can really substitute the real controls. Instead I find those problematic. Why:

    • I loose the rich design time support of VS.
    • Certain but very common scenario like bind some data source with a select list with some predefined items what the AppendDataBoundItems property of the List Control does in web form world. Or, consider how do you generate a Url with # with those helpers.
    • Currently helper exists for intrinsic controls, but did you consider the number of parameters when you introduce helper methods for GridView/DataList/Template supported controls.
    Since most of the existing controls of ASP.NET become handicapped in MVC world, why not develop a separate set of controls for MVC, it is hard to imagine running VS without toolbox.

  • Prior MIX08, ScottGu posted the roadmap of mix08 release. I would like to see a detail list of features that are planned for the final version. Certainly, a lot of things will be added/modified from the community feedback.

kick it on DotNetKicks.com

More Posts