DEV Community


Fixing browser caching problems in ASP.NET Core

Armen Hovsepian
I'm enthusiastic about web development.
・1 min read

In order to fix browser caching in ASP.NET Core you can use "asp-append-version" tag helper in your website images, CSS links, and javascript references. If you use this tag helper and set it true a version suffix will be appended to your resource and when you change the file on the server the version of the file will be changed and the browser noticed and automatically downloads the new version as well.
Here are some examples:

<img src="~/images/banner1.jpg" width="100%" asp-append-version="true"/>

<link href="~/css/site.css" rel="stylesheet" asp-append-version="true"/>

<script src="~/js/site.js" asp-append-version="true"></script>

 // Result
<img src="/images/banner1.jpg?v=jGW_rax5TkGDuL0z_GnDkSZTmmFqdGRzA8pXnJnug7c" width="100%">

<link href="/css/site.css?v=t2uNA6b681W1KvNxE6O-pzzaJGt2penW9dzO1CxKNns" rel="stylesheet"/>

<script src="/js/site.js?v=ji3-IxbEzYWjzzLCGkF1KDjrT2jLbbrSYXw-AhMPNIA"></script>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)