Tuesday, May 26, 2009

ASP.NET MVC Output Caching

I've found what I believe to be a bug in ASP.NET MVC's caching model inasmuch as it appears to be caching GET's when caching is not specified. However, even though I think that it's a bug it's way more likely that this is something that I haven't understood or something that I've done wrong: The first rule of programming: It's my fault.

I've written a wizard style registration process for a site. It's fairly simple and stores the stage at which the user is at in a session state variable. Here is the code: 

[OutputCache(Duration=0, VaryByParam="None")]
public ActionResult Register()
{
    string registerStage = GetStage();

    switch (registerStage)
    {
        case "1":
            return View("Register1");
        case "2":
            return View("Register2", UserDetailsVM);
        case "3":
            return View("Register3", UserDetailsVM);
        case "4":
            return View("CancelClicked");
        default:
            return View("NotFound");
    }
}

What I found was that without the [OutputCache(Duration=0, VaryByParam="None")] attribute it appeared that the view was being cached.

If I removed this attribute and called the action via a browser it would work and break points inside the action would be hit the first time I navigated to the action/page. If I then navigated to a web form page (this is a hybrid web form / mvc application) and then returned to this page the Register action would not be hit. Added the [OutputCache(Duration=0,...] fixed the problem.

3 comments:

  1. Really helpful and I found this after wasting 2 days behind this but worth it totally ... thanks and nice post...
    Bhavin

    ReplyDelete
  2. Really helpful and I found this after wasting 2 days behind this but worth it totally ... thanks and nice post...
    Bhavin

    ReplyDelete
  3. Thnaks... I don´t know what was happening.

    ReplyDelete