views:

168

answers:

1

I'm fairly new to using the OutputCache attribute in ASP.NET MVC.


Static Pages

I've enabled it on static pages on my site with code such as the following:

[OutputCache(Duration = 7200, VaryByParam = "None")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        //...

If I understand correctly, I made the whole controller cache for 7200 seconds (2 hours).


Dynamic Pages

However, how does it work with dynamic pages? By dynamic, I mean where the user has to submit a form.

As an example, I have a page with an email form. Here's what that code looks like:

public class ContactController : Controller
{
    //
    // GET: /Contact/

    public ActionResult Index()
    {
        return RedirectToAction("SubmitEmail");
    }

    public ActionResult SubmitEmail()
    {
        //In view for CAPTCHA: <%= Html.GenerateCaptcha() %>
        return View();
    }

    [CaptchaValidator]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SubmitEmail(FormCollection formValues, bool captchaValid)
    {
        //Validate form fields, send email if everything's good...

            if (isError)
            {
                return View();
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

    }

    public void SendEmail(string title, string name, string email, string message)
    {
        //Send an email...

    }
}

What would happen if I applied OutputCache to the whole controller here?

Would the HTTP POST form submission work? Also, my form has a CAPTCHA; would that change anything in the equation?

In other words, what's the best way to approach caching with dynamic pages?

Thanks in advance.

+1  A: 

There's a good article on post-cache substitution here:

http://www.asp.net/mvc/tutorials/adding-dynamic-content-to-a-cached-page-cs

I think that's what you're looking for.

stupid-phil
How does it influence HTTP POST form submissions, though?
Maxim Zaslavsky
Caching is about the output not the input. So the POST wont be affected. However if you cache the output, then no matter what the input, the page will always return the same results, unless you use post-cache substitution or a similar solution.
stupid-phil
So, about the GET part: would that affect the CAPTCHA, the validation, or both? Just making sure I completely understand...
Maxim Zaslavsky