在MVC Last-Modified头 [英] Last-Modified Header in MVC

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

问题描述

我最近遇到的Last-Modified头。

  • 如何以及在哪里可以在MVC中包括吗?
  • 什么是包括它的优势是什么?

我希望有一个例子,如何最后修改标头可以被包含在一个MVC项目,静态网页和数据库查询呢?

时它的OutputCache不同,如果是的话怎么样?

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

解决方案

的Last-Modified 主要用于缓存

。它发回的资源,你可以跟踪的修改时间。资源不必是文件,但任何东西。对于那些从分贝的信息,你有一个 UpdatedAt 列生成实例的页面。

及其与如果修改 - 因为报头中的请求,每个浏览器发送(组合使用,如果它已经收到了的Last-Modified 头previously)。

  

如何以及在哪里可以在MVC中包括了?

Response.AddHeader

  

什么是包括它的优势是什么?

启用细粒度高速缓存这是动态生成的网页(例如,你可以使用你的数据库字段 UpdatedAt 作为最后修改的头部)。

示例

为使一切工作,你必须做一些事情是这样的:

 公共类YourController:控制器
{
    公众的ActionResult我的页面(串号)
    {
        变种实体= _db.Get(ID);
        VAR headerValue = Request.Headers ['如果修改 - 因为'];
        如果(headerValue!= NULL)
        {
            变种modifiedSince = DateTime.Parse(headerValue).ToLocalTime();
            如果(modifiedSince> = entity.UpdatedAt)
            {
                返回新的HTTPStatus codeResult(304,页面没有被修改);
            }
        }

        //页面已被改变。
        //生成视图...

        // ..并设置最后修改在HTTP RFC中指定的日期格式。
        Response.AddHeader(上次修改,entity.UpdatedAt.ToUniversalTime()的ToString(R));
    }
}
 

您可能必须指定在DateTime.Parse的格式。

参考文献:

Disclamer :我不知道,如果ASP.NET/MVC3支持您管理的Last-Modified 自己

更新

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

 公共静态类CacheExtensions
{
    公共静态布尔通过isModified(此控制器控制,日期时间updatedAt)
    {
        VAR headerValue = controller.Request.Headers ['如果修改 - 因为'];
        如果(headerValue!= NULL)
        {
            变种modifiedSince = DateTime.Parse(headerValue).ToLocalTime();
            如果(modifiedSince> = updatedAt)
            {
                返回false;
            }
        }

        返回true;
    }

    公共静态的ActionResult NotModified(此控制器控制器)
    {
        返回新的HTTPStatus codeResult(304,页面没有被修改);
    }
}
 

然后用它们是这样的:

 公共类YourController:控制器
{
    公众的ActionResult我的页面(串号)
    {
        变种实体= _db.Get(ID);
        如果(!this.IsModified(entity.UpdatedAt))
            返回this.NotModified();

        //页面已被改变。
        //生成视图...

        // ..并设置最后修改在HTTP RFC中指定的日期格式。
        Response.AddHeader(上次修改,entity.UpdatedAt.ToUniversalTime()的ToString(R));
    }
}
 

I have recently come across the Last-Modified Header.

  • How and where can I include it in MVC?
  • What are the advantages of including it?

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

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.

解决方案

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.

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

How and where can I include it in MVC?

Response.AddHeader

What are the advantages of including it?

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

Example

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"));
    }
}

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

References:

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

Update

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");
    }   
}

And then use them like this:

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头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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