简体   繁体   English

带有POST控制器操作的ASP.NET MVC OutputCache

[英]ASP.NET MVC OutputCache with POST Controller Actions

I'm fairly new to using the OutputCache attribute in ASP.NET MVC. 我在ASP.NET MVC中使用OutputCache属性相当新。


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). 如果我理解正确,我将整个控制器缓存7200秒(2小时)。


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? 如果我在这里将OutputCache应用到整个控制器会发生什么

Would the HTTP POST form submission work? HTTP POST表单提交是否有效? Also, my form has a CAPTCHA; 另外,我的表格有一个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. 提前致谢。

By taking advantage of output caching, you can dramatically improve the performance of an ASP.NET MVC application. 通过利用输出缓存,您可以显着提高ASP.NET MVC应用程序的性能。 Instead of regenerating a page each and every time the page is requested, the page can be generated once and cached in memory for multiple users. 不是每次请求页面时都重新生成页面,而是可以生成一次页面并将其缓存在内存中以供多个用户使用。

First of the scenario you are going to implement is not proper. 您要实施的第一个方案是不正确的。 Keep one thing in mind the output cache should be used only at time where it does not affect your business logic, You wanted to reduce sever load & Sql data Retrieval of frequently used page but less frequently update data. 请记住,输出缓存只应在不影响业务逻辑的情况下使用。您希望减少服务器负载和Sql数据检索常用页面但更不频繁更新数据。

Fortunately, there is an easy solution. 幸运的是,有一个简单的解决方案。 You can take advantage of a feature of the ASP.NET framework called post-cache substitution. 您可以利用ASP.NET框架的一项称为后缓存替换的功能。 Post-cache substitution enables you to substitute dynamic content in a page that has been cached in memory. 缓存后替换使您可以替换已缓存在内存中的页面中的动态内容。

http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/adding-dynamic-content-to-a-cached-page-cs http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/adding-dynamic-content-to-a-cached-page-cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM