My question is ,
Can i clean Browser cache/history through code(c#).
because, Browser always stored previous angular/css.
Everytime, we have to clear manually(By Cntrl+Shift+Del).
Is there any C# Code to clear history with button click.
Thank you.
- 77
- 12
-
4You need to fix the problem, not automate cache clearing. – DavidG Dec 03 '19 at 13:18
-
1[You want to investigate what is called cache busting](https://css-tricks.com/strategies-for-cache-busting-css/) – Crowcoder Dec 03 '19 at 13:19
-
@DavidG, you mean ,there is no way through coding.. – DOTNET Dev Dec 03 '19 at 13:21
-
are you using asp.net core? – rjs123431 Dec 03 '19 at 13:21
-
@rjs123431 only .net – DOTNET Dev Dec 03 '19 at 13:23
-
No, I mean use cache busting e.g. https://stackoverflow.com/questions/39647810/how-to-prevent-browser-cache-on-angular-2-site – DavidG Dec 03 '19 at 13:24
-
you mention about C#, angular/css, it means this is a web application .net framework, it that correct? – rjs123431 Dec 03 '19 at 13:24
-
yes @rjs123431. – DOTNET Dev Dec 03 '19 at 13:25
-
1okay, so you don't need to clear the cache, possible solution in .net mvc is to append a version of the js and css files – rjs123431 Dec 03 '19 at 13:31
3 Answers
Can try like this:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
- 11
- 2
Your issue is about the css and js files are cached in the browser.
Please look at the default template of Asp Mvc Application.
_Layout.cshtml should have this at the bottom
@Scripts.Render("~/bundles/jquery")
In BundleConfig.cs, you need to add this at the bottom of the RegisterBundles method
BundleTable.EnableOptimizations = true;
You should see the source of the web page the scripts have some extra version
<script src="/bundles/jquery?v=2u0aRenDpYxArEyILB59ETSCA2cfQkSMlxb6jbMBqf81"></script>
In this way, whenever you have changes in your js or css files, version will change so browser will treat this as another file instead of the previous cached version of js or css files.
In Asp.Net Core MVC, it is much easy using asp-append-version
<script src="~/js/site.js" asp-append-version="true"></script>
- 593
- 2
- 12
Thanks For The Answers but Finally best solution for This question for me.
In _Layout.cshtml, Add a version Where We Linked The Path of Angular and Css files. Browser will detect this is a new File and Then replace it. Now clear History/cache is not needed. Code: ?v=1
example:
<script src="~/Scripts/angularcontroller.js?v=1"></script>
<script src="~/Css/style.css?v=1"></script>
- 77
- 12