MVC 中的 Last-Modified Header [英] Last-Modified Header in MVC

查看:34
本文介绍了MVC 中的 Last-Modified Header的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了 Last-Modified Header.

I have recently come across the Last-Modified Header.

  • 如何以及在何处将其包含在 MVC 中?
  • 加入它有什么好处?

我想要一个示例,如何将最后修改的标头包含在 mvc 项目中,用于静态页面和数据库查询?

I want an example how last modified header can be included in an mvc project, for static pages and database queries as well?

它和 outputcache 有什么不同,如果有的话怎么做?

Is it different from outputcache, if yes how?

基本上,我希望浏览器清除缓存并自动显示最新的数据或页面,而无需用户进行刷新或清除缓存.

Basically, I want the browser to clear the cache and display the latest data or pages automatically, without the need for the user to do a refresh or clearing the cache.

推荐答案

Last-Modified 主要用于缓存.它被发回用于您可以跟踪修改时间的资源.资源不一定是文件,而是任何东西.例如从 dB 信息生成的页面,其中您有一个 UpdatedAt 列.

The Last-Modified is mainly used for caching. It's sent back for resources for which you can track the modification time. The resources doesn't have to be files but anything. for instance pages which are generated from dB information where you have a UpdatedAt column.

它与每个浏览器在请求中发送的 If-Modified-Since 标头结合使用(如果它之前收到了 Last-Modified 标头).

Its used in combination with the If-Modified-Since header which each browser sends in the Request (if it has received a Last-Modified header previously).

如何以及在何处将其包含在 MVC 中?

How and where can I include it in MVC?

Response.AddHeader

加入它有什么好处?

为动态生成的页面启用细粒度缓存(例如,您可以使用数据库字段 UpdatedAt 作为最后修改的标头).

Enable fine-grained caching for pages which are dynamically generated (for instance you can use your DB field UpdatedAt as the last modified header).

示例

要使一切正常,您必须执行以下操作:

To make everything work you have to do something like this:

public class YourController : Controller
{
    public ActionResult MyPage(string id)
    {
        var entity = _db.Get(id);
        var headerValue = Request.Headers["If-Modified-Since"];
        if (headerValue != null)
        {
            var modifiedSince = DateTime.Parse(headerValue).ToLocalTime();
            if (modifiedSince >= entity.UpdatedAt)
            {
                return new HttpStatusCodeResult(304, "Page has not been modified");
            }
        }

        // page has been changed.
        // generate a view ...

        // .. and set last modified in the date format specified in the HTTP rfc.
        Response.AddHeader("Last-Modified", entity.UpdatedAt.ToUniversalTime().ToString("R"));
    }
}

您可能需要在 DateTime.Parse 中指定格式.

You might have to specify a format in the DateTime.Parse.

免责声明:我不知道 ASP.NET/MVC3 是否支持您自己管理 Last-Modified.

Disclamer: I do not know if ASP.NET/MVC3 supports that you manage Last-Modified by yourself.

更新

您可以创建一个扩展方法:

You could create an extension method:

public static class CacheExtensions
{
    public static bool IsModified(this Controller controller, DateTime updatedAt)
    {
        var headerValue = controller.Request.Headers['If-Modified-Since'];
        if (headerValue != null)
        {
            var modifiedSince = DateTime.Parse(headerValue).ToLocalTime();
            if (modifiedSince >= updatedAt)
            {
                return false;
            }
        }

        return true;
    }

    public static ActionResult NotModified(this Controller controller)
    {
        return new HttpStatusCodeResult(304, "Page has not been modified");
    }   
}

然后像这样使用它们:

public class YourController : Controller
{
    public ActionResult MyPage(string id)
    {
        var entity = _db.Get(id);
        if (!this.IsModified(entity.UpdatedAt))
            return this.NotModified();

        // page has been changed.
        // generate a view ...

        // .. and set last modified in the date format specified in the HTTP rfc.
        Response.AddHeader("Last-Modified", entity.UpdatedAt.ToUniversalTime().ToString("R"));
    }
}

这篇关于MVC 中的 Last-Modified Header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆