Whenever user access any website - images, JS and CSS
are cached by browser. But if JS and CSS are updated on server still client browser
refer the old JS and CSS. That’s very bad. It’s always good practice to provide
the latest copy of CSS and JS to client browser.
But how to do it in ASP.NET to make sure browser is
getting the updated files.
To solve this, we can use query string with JS and CSS.
For CSS, take one literal in <Head> section of
aspx page as below
<head runat="server">
<asp:Literal ID="ltStyleCss" runat="server" Text="" ></asp:Literal>
</head>
Write below code
public string CurrentVersion = "";
protected void Page_Load(object sender, EventArgs e)
{
CurrentVersion = DateTime.Today.ToString("MMddyyyy") + "_" + DateTime.Now.Hour.ToString();
ltStyleCss.Text = string.Format("<link
href=\"/ css/style.css?ver={0}\" rel=\"stylesheet\"
/>", CurrentVersion) + System.Environment.NewLine;
Here we are declaring version for each hour of the day.
You can do it for each request by specifyin current
datetime, but problem with that approach is that it affects network bandwidth.
So per hour solution is better.
It’s very easy to specify it for JS by directly passing
the version variable as query string
<script src="/Presentation/scripts/jquery.js?ver=<%=CurrentVersion%>"></script>
Pretty simple for JS but you can’t achieve the same way
for CSS. Let me know if you can do it for CSS as well.
Please leave your comments or share this tip if it’s
useful for you.
The client was very satisfied with the work done, web designing studios
ReplyDeleteproject management, and ability to stay on a project timeline.
The team excelled at thoroughly understanding the requirements. They met every guideline and brainstormed creative solutions.
ReplyDeletebrand design studio
The team has met all deadlines and exceeded our expectations for scheduling.
ReplyDeletedigital branding agency
Their small size helped them deliver quick turnarounds and seamless collaboration.
ReplyDeletetop app design companies
Their developers are flexible, extremely flexible, and talented.
ReplyDeletebest UX companies
The team leverages their experience and expertise to deliver quality results.
ReplyDeletebest UX design
The company was most impressed by the amount of creative and branding talent that the team had at their disposal for this project.
ReplyDeleteuser experience design companies
Excellent Blog! I have been impressed by your thoughts and the way you
ReplyDeleteSan Francisco design firms
Your articles and contents are encouraging.
ReplyDeleteweb designer San Francisco
I don’t suppose many of websites give this kind of information.
ReplyDeleteUI team
I think this is a charming issue, I expect you would surely post on it again sometime near the future. Thanks guys!
ReplyDeletecustom logo designers
If you really desire to get such type of information, visit this blog quickly.
ReplyDeletetop 10 website designing company
This article has some vast and valuable information about this subject.
ReplyDeleteweb design studio
Your blogs and its stuff are so notable and worthwhile it can make me return.
ReplyDeleteproduct design agency
Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work.
ReplyDeleteweb design agencies calgary ab