Caching not working in MVC 5 application - asp.net-mvc

I am struggling in implementing caching in my MVC application.
Below is the code I have added in my root directory web.config file
<system.webServer>
<caching>
<profiles>
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:20:00" location="Client" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:20:00" location="Client" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:20:00" location="Client" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:20:00" location="Client" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:02:00" location="Client" />
</profiles>
</caching>
<staticContent>
<clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Sat, 11 June 2016 00:00:00 GMT" cacheControlCustom="public" />
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="text/javascript" />
</staticContent>
<urlCompression doDynamicCompression="true" doStaticCompression="true" />
<httpCompression dynamicCompressionEnableCpuUsage="80" />
<httpProtocol allowKeepAlive="true">
<customHeaders>
<add name="ETag" value="0" />
</customHeaders>
</httpProtocol>
</system.webServer>
Questions::
1)When I am checking the output via chrome developer tools network sections it is always taking images from server and not cache.I always get Status 200 response.I am using around 12 images in a sprite and they will never change.
2) I am not able to figure out how js and css bundles are cached.Every round trip again loads the complete bundle from server.Sometimes I get 304 response but I am not able to understand this fluctuating behaviour.
I am not able to drill down the issue.I have searched everywhere including this site but not able to put the optimum config settings to achieve the output.
Kindly someone help

Related

cache static images by keeping Cache-Control set to no cache in asp.net mvc

I have images in my ~/Content/Images folder. And these images are used in Layout and other pages. When i request a page "Contact", it loads images referenced on that page and i can see requests in IIS log for each image load. This is ok for the first request. and for 2nd and further requests to contact page, i do not want these images to be requested from server again, want them to be load from cache.
I don’t want to change security settings in my web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
</system.webServer>
I am trying to add output cache setting for images as
<system.webServer>
<caching>
<profiles>
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
</profiles>
</caching>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
</system.webServer>
But above setting still sending a requests for images each time I am refreshing the page.
do not want session values to be cached so I set cache-control to no-cache.
I want to stop these requests for static images until I change it.
What i am missing here?

Glimpse Heads Up Display is not showing up when debugging in VS 2017

I installed Glimpse in my Web project that is using MVC5 via Nuget. I am using Visual Studio 2017 with Resharper. I also am running through IIS so it is giving me back app.company.local rather than using IIS Express and localhost. Could that be the problem?
I have gone through the troubleshooting documentation as well as quite a few questions on here but Glimpse's HUD is not showing up for some reason.
Below are some if not all of the changes made to try and get it to work.
packages.config
<package id="Glimpse" version="1.8.6" targetFramework="net461" />
<package id="Glimpse.Ado" version="1.7.3" targetFramework="net461" />
<package id="Glimpse.AspNet" version="1.8.0" targetFramework="net461" />
<package id="Glimpse.EF6" version="1.6.5" targetFramework="net461" />
<package id="Glimpse.Mvc5" version="1.5.3" targetFramework="net461" />
index.cshtml
added #using Glimpse.Mvc.Html to the top and #Html.GlimpseClient() to the bottom
(there are a bunch of divs and no body tag)
Web.config
<add key="Glimpse:DisableAsyncSupport" value="true" />
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" /></httpModules>
<add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" />
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" /></modules>
<add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" /></handlers>
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<add type="Glimpse.Mvc.Inspector.DependencyInjectionInspector, Glimpse.Mvc5" />
<add type="Glimpse.AspNet.Policy.LocalPolicy, Glimpse.AspNet"/>
What do I need to do in order to get this to work/show up?
There should be no problem for company.local.
You would just need to go to:
app.company.local/Glimpse.axd
To get to you config display. Then
You have not enabled Glimpse in the web.config, by setting defaultRuntimePolicy to On:
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
...
</glimpse>

Sitecore MVC - gzip compression error

I'm experiencing a problem when using Sitecore MVC 3 rendering with GZip content compression.
I followed the blog post of John West, how to enable MVC in Sitecore.
Until now it works perfectly, the pages are rendered. But if I run the page on IIS and enable content compression (gzip), the page doesn't load. I get a "Content Encoding Error" in Firefox. Other browser display various error messages.
Has somebody experienced similar issues? Do you have any idea what the problem may be? Where should I start checking? I have to use compression on the pages.
We are using Sitecore 6, Update 5: "Sitecore 6.6.0 rev. 130404"
Could this be a Sitecore bug?
EDIT 1: I am also running ASP.NET WebForms on the Sitecore instance and it works fine also with gzip compression.
EDIT 2: I have 'dynamicCompressionBeforeCache' enabled. My web.config related to gzip config:
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
Sitecore confirmed that they can reproduce the issue. When setting dynamicCompressionBeforeCache="true", the encoding does not work correctly for some reason.
One solution is to remove this setting. After applying dynamicCompressionBeforeCache="false" it works fine.
You should probably enable gzip in your web.config
<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\
temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>
There are a few more tricks here
Setting the gzip compression in asp.net

Issues facing working with GZip

I am facing a problem while trying to use GZip .
I have used the below entries in my config files i.e., applicationHost.config and web.config
<httpCompression>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" dynamicCompressionLevel="4" />
</httpCompression>
<urlCompression doDynamicCompression="true" doStaticCompression="true"/>
I have my application being deployed on IIS 7.5.
I have seen these suggestions over the net and I happen to hear that I would not need an exclusive stream code to be written. An entry to config file would suffice.
But still I get an error when my WCF service returns me a collection object with records over 1000. The object is a serializable object.
Please help me in this issue

Why am I getting an error in my web.config file

I have the following section added to my web.config:
<system.webServer>
<caching>
<profiles>
<add extension=".html" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".htm" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
</profiles>
</caching>
</system.webServer>
which was taken directly from here
but I am getting an error in my web.config, saying:
Unrecognized configuration section system.web/caching/profiles
also, I see this in visual studio:
The element caching has invalid child element 'profiles', List of possible expected: cache, outputcache, outputcacheSettings, sqlCacheDependency'
did something about this configuration change as I see alot of examples on the web with this exact configuration?
In your screenshot you have the caching under <system.web> and not <system.webServer> and since profiles is not a valid element under <caching> for <system.web> you will get that error.
What you show in code example is under <system.webServer>, what you show in your screenshot is under <system.web>, it needs to be under webServer for it to get the profiles option, under system.web like you had it offers all those options that the error you received was talking about, behaves differently depending on where you call it from

Resources