ASP.NET MVC OutputCache with POST Controller Actions
- by Maxim Z.
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.