Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last-Modified Header in MVC

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.

like image 265
learning Avatar asked May 08 '12 11:05

learning


2 Answers

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:

  • HTTP status codes
  • HTTP headers

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"));     } } 
like image 182
jgauffin Avatar answered Oct 02 '22 20:10

jgauffin



UPDATE: Check my new answer


How and where can I include it in MVC?

The built-in OutputCache filter does the job for you and it uses those headers for caching. The OuputCache filter uses the Last-Modified header when you set the Location as Client or ServerAndClient.

[OutputCache(Duration = 60, Location = "Client")] public ViewResult PleaseCacheMe() {     return View(); } 

What are the advantages of including it?

Leveraging client-side caching with conditional cache flush

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

This link contains enough information to try out a sample. For static pages like html, images IIS will take care of setting/checking the Last-Modified header and it uses the file's last modified date. For database queries you can go for setting the SqlDependency in the OutputCache.

Is it different for outputcache, if yes how? When do I need to include Last-Modified Header and when to use outputcache?

OutputCache is an action filter used for implementing caching mechanism in ASP.NET MVC. There are different ways you could perform caching using OutputCache: client-side caching, server-side caching. Last-Modified header is one way to accomplish caching in the client-side. OutputCache filter uses it when you set the Location as Client.

If you go for client-side caching (Last-Modified or ETag) the browser cache will automatically get updated in subsequent request and you don't need to do F5.

like image 29
VJAI Avatar answered Oct 02 '22 21:10

VJAI