You can include it in MVC anywhere you can get access to the Cache object, which is typically through the Response object:
Response.Cache.SetLastModified(someDateTime);
The advantage is it allows clients that have visited your page before to not download static files (like images that don't change) they have in browser cache, so the site will seem faster.
When a visitor browses your website, his/her browser caches your images, html, style sheet and other files locally for better performance. If you have the so-called "Last-Modified" header set in your pages, next time when the same visitor loads your website, the server will check whether there is a change in the files since the last time they have been accessed.
If there is no change, the server will send a "304 Not Modified" reply to the browser and the information will be loaded from the local cache. If you do not update your website very often, using "Last-modified" headers can significantly increase its loading speed.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Darell, thanks for the explanation. Do I have to include it per action or per controller? In my project I am having some 7 controller, each with some 5 actions minimum. Where should I include it? What is someDateTime? How Do I determine that value? Is
it some fixed values in date, time, seconds.
Darell, thanks for the explanation. Do I have to include it per action or per controller? In my project I am having some 7 controller, each with some 5 actions minimum. Where should I include it? What is someDateTime? How Do I determine that value? Is
it some fixed values in date, time, seconds.
public static class CacheExtensions
{
public bool IsModified(Controller controller, DateTime updatedTime)
{
var header = controller.Request.Headers['If-Modified-Since'];
if (header != null)
{
var modifiedTime = DateTime.Parse(header).ToLocalTime();
if (modifiedTime >= updatedTime)
{
return false;
}
}
return true;
}
public ActionResult NotModified(Controller controller)
{
return new HttpStatusCodeResult(304, "Page has not been modified");
}
}
Then:
public class YourController : Controller
{
public ActionResult YourAction(string id)
{
var entity = _db.Get(id);
if (!this.IsModified(entity.UpdatedTime))
return this.NotModified();
// ...
Response.AddHeader('Last-Modified', entity.UpdatedTime.ToUniversalTime().ToString("R"));
}
}
Hope this helpful
Regards
Please mark the replies as answers if they help or unmark if not.
Feedback to us
mim
Member
60 Points
146 Posts
Last-Modified Header in MVC
May 17, 2012 09:54 AM|LINK
I have recently come across the Last-Modified Header.
DarrellNorto...
All-Star
86665 Points
9634 Posts
Moderator
MVP
Re: Last-Modified Header in MVC
May 17, 2012 10:12 AM|LINK
You can include it in MVC anywhere you can get access to the Cache object, which is typically through the Response object:
The advantage is it allows clients that have visited your page before to not download static files (like images that don't change) they have in browser cache, so the site will seem faster.
From here:
Last-Modified HTTP header explained
When a visitor browses your website, his/her browser caches your images, html, style sheet and other files locally for better performance. If you have the so-called "Last-Modified" header set in your pages, next time when the same visitor loads your website, the server will check whether there is a change in the files since the last time they have been accessed.
If there is no change, the server will send a "304 Not Modified" reply to the browser and the information will be loaded from the local cache. If you do not update your website very often, using "Last-modified" headers can significantly increase its loading speed.
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
ignatandrei
All-Star
134903 Points
21619 Posts
Moderator
MVP
Re: Last-Modified Header in MVC
May 17, 2012 10:13 AM|LINK
I think that you can include more in WebAPI rather in MVC
see an example for etag here : http://tudorturcu.wordpress.com/
In pure MVC - none. This is more for browsers rather than for MVC
mim
Member
60 Points
146 Posts
Re: Last-Modified Header in MVC
May 17, 2012 11:19 AM|LINK
Darell, thanks for the explanation. Do I have to include it per action or per controller? In my project I am having some 7 controller, each with some 5 actions minimum. Where should I include it? What is someDateTime? How Do I determine that value? Is it some fixed values in date, time, seconds.
mim
Member
60 Points
146 Posts
Re: Last-Modified Header in MVC
May 18, 2012 10:56 AM|LINK
Darell, thanks for the explanation. Do I have to include it per action or per controller? In my project I am having some 7 controller, each with some 5 actions minimum. Where should I include it? What is someDateTime? How Do I determine that value? Is it some fixed values in date, time, seconds.
</div>Young Yang -...
All-Star
21344 Points
1818 Posts
Microsoft
Re: Last-Modified Header in MVC
May 24, 2012 02:07 AM|LINK
You can create an extension method, like this:
public static class CacheExtensions { public bool IsModified(Controller controller, DateTime updatedTime) { var header = controller.Request.Headers['If-Modified-Since']; if (header != null) { var modifiedTime = DateTime.Parse(header).ToLocalTime(); if (modifiedTime >= updatedTime) { return false; } } return true; } public ActionResult NotModified(Controller controller) { return new HttpStatusCodeResult(304, "Page has not been modified"); } }Then:
public class YourController : Controller { public ActionResult YourAction(string id) { var entity = _db.Get(id); if (!this.IsModified(entity.UpdatedTime)) return this.NotModified(); // ... Response.AddHeader('Last-Modified', entity.UpdatedTime.ToUniversalTime().ToString("R")); } }Hope this helpful
Regards
Feedback to us
Develop and promote your apps in Windows Store