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

发布于 2024-09-03 03:38:29 字数 1468 浏览 5 评论 0原文

我对在 ASP.NET MVC 中使用 OutputCache 属性还很陌生。


静态页面

我已经在我的网站上的静态页面上启用了它,代码如下:

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

如果我理解正确,我将整个控制器缓存了 7200 秒(2 小时)。


动态页面

但是,它如何与动态页面一起工作呢? 动态,我的意思是用户必须提交表单。

例如,我有一个包含电子邮件表单的页面。该代码如下所示:

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...

    }
}

如果我将 OutputCache 应用于整个控制器,会发生什么?

HTTP POST 表单提交有效吗?另外,我的表单有验证码;这会改变等式中的任何东西吗?

换句话说,使用动态页面进行缓存的最佳方法是什么?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-09-10 03:38:29

通过利用输出缓存,您可以显着提高 ASP.NET MVC 应用程序的性能。页面可以生成一次并缓存在内存中以供多个用户使用,而不是每次请求页面时都重新生成页面。

首先,您要实施的场景是不正确的。请记住一件事,输出缓存应该仅在不影响您的业务逻辑的情况下使用,您想要减少服务器负载和服务器负载。 Sql数据检索经常使用的页面但不经常更新的数据。

幸运的是,有一个简单的解决方案。您可以利用 ASP.NET 框架的一项称为缓存后替换的功能。缓存后替换使您能够替换已缓存在内存中的页面中的动态内容。

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

By taking advantage of output caching, you can dramatically improve the performance of an ASP.NET MVC application. 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.

Fortunately, there is an easy solution. You can take advantage of a feature of the ASP.NET framework called post-cache substitution. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文