Check whether you are getting cached content in MVC website

The ways to check whether you are viewing cached or most recent content

It happens from time to time that you publish some content from back-end, but on the website you still see the old content.

Reasons for this may be different:

  • Something went wrong in code while publishing
  • Content is cached by proxy
  • Something is wrong with DNS and does not point to the real IP address (you might be running website on two locations during migration from machine to another)

In case you have load balancer and two application server instances, you would probably want to know from which machine you are getting the response. The following simple RAZOR code can help you.

<meta name="generator" content="@System.Environment.MachineName @DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")" />
    

I saved this small piece of code as a partial view and then used it on other pages like that

@Html.Partial("GeneratorMeta")
    

It is pretty simple but pretty useful thing and it does not affect your website layout or content. Just add it inside head of your HTML document and on response you will get source machine and time when response is generated.

<meta name="generator" content="DEJAN-PC 2014-09-04 15:09:14" />
    

You can easily ad any additional info you might need for your response info.

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article