Enable all caches except asp net output cache - asp.net

I have different urls that points to the same code
www.url1.com
www.url2.com
I need to use the cache, but if the asp net cache is enabled when someone access to www.url1.com next person accessing www.url2.com could get the previously cached data (www.url1.com)
I need to have ALL caches activated except this one.

You can disable ASP.Net output caching for the entire application by putting it in your web.config file.
<configuration>
<system.web>
<caching>
<outputCache enableOutputCache="false">
</outputCache>
</caching>
</system.web>
</configuration>
But unless you're actually putting anything in the cache in the first place, you don't have anything to worry about.

Related

How to turn off session state for a certain directory?

I have a custom session provider that stores data in a DB, so fetching session state is not a cheap operation. I have also runAllManagedModulesForAllRequests="true" in my web.config, so sessions are inicialized for every request to an image.
So, I am striving for turning off session state for directories with static resources. Unfortunately, setting <sessionState mode="Off" /> for a folder does not work as this is application-level option.
I could handle this in the code of my custom session provider, but I am wondering if there is some cleaner, more declarative solution.
Edit: <pages enableSessionState="false" /> does not seem to help as it is probably related to ASP.NET pages and controls only.
I don't think that is possible, think about it as session is provided for the whole application, and you just can say at this folder i have and at that i don't, all you can do is to custom code the costly operation of your provider to not run according to certain conditions, place that correctly in your page life cycle.
I ran into the same problem trying to do WebServices with cookieless sessions. I solved it with using a symbolic link. Here are the steps:
1) Make your subdirectory its own application in IIS
2) put this in the subdirectory web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<sessionState mode="Off" />
</system.web>
</configuration>
3) Create a symbolic link to redirect BIN to your own BIN. In my example the subdirectory was only 1 level below my main application. So I did this command from the subdirectory:
mklink /D bin ..\bin
That's it. It will load assemblies from your main BIN directory, and inherit all its other settings from your main application.

Output Cache not caching all pages

I have a cache rule in a directory by file extension and there are 2 dynamic pages in that directory.
<configuration>
<system.webServer>
<caching>
<profiles>
<add extension=".asp"
policy="CacheForTimePeriod"
kernelCachePolicy="DontCache"
duration="03:00:00"
location="Any"
varyByQueryString="*" />
</profiles>
</caching>
</system.webServer>
</configuration>
There really isn't a significant difference between the two, they both run off querystrings, however, IIS doesnt want to cache one of them for some reason I can't figure out why. One of the pages it caches perfectly, but the other it doesn't. I was thinking it may have been due to the output filesize, since the page being cached is always under 256kb and the other is usually over, and 256kb is the default setting in MaxCachedFileSize. So I added the following registry as per ms suggestions:
HKLM\System\CurrentControlSet\Services\InetInfo\Parameters\MaxCachedFileSize
I set it to 10485760 bytes (10mb), since default is 256kb. Still not caching. As I said, the other file in the same dir caches fine, but the other one doesn't. Not sure what it could be, there is barely any memory being used on the server, so I don't think this can be a memory space not available issue.
Also, I've read that sending cookies in the page can cause IIS not to cache the page, but neither of the pages are sending cookies.
Removing Response.Flush from the page did the trick for anyone else in the future.

IIS and Static content?

According to Ultra-Fast ASP.NET: Chapter 3 - Caching:
Files that the browser retrieves from the server should be stored in
the browser’s cache as long as possible to help minimize server
round-trips.
But how does IIS know what a static content actually is and is not?
Is it just images, CSS, JS and not ASPX, ashx...?
Where can I see in IIS what is already considered to be static and what is not ?
What about the scenario where a page has been declared with <%# OutputCache header (without location)? Are the images, CSS and JS source files inside of it also being output cached with the same properties?
As a best practice, I should set one year into the future as the maximum expiration time. I should use that as the default for all static content on the site
So I did this :
But later, after pressing OK, I can't find any summary menu which shows me: to whom I already put a response header (in this case: the css folder).
Currently, in order to see that css folder has been applied with response headers - I have to go to the css folder again --> Http Response Header-->Set Common Headers --> and then I see it. It isn't written in the web.config.
But if I do it for a file (Login.aspx for example): I do see it in web.config:
<configuration>
<location path="Login.aspx">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Fri, 15 Feb 2013 00:00:00 GMT" />
</staticContent>
</system.webServer>
</location>
</configuration>
I understand your situation. Sometime its confusing how IIS handles a file. Its also different for IIS 6 vs IIS 7 and different for Classic App Pools and Integrated mode app pools. My experience is mostly with Integrated App Pools on IIS 7.5, so thats the environment I can comment on most accurately.
First Question
But how does IIS knows what is actually a static content and what is
not?
Is it just images , css , js and not ASPX , ashx...?
Where can I see in the IIS what is already considered to be static and
what not ?
You can inspect the list of file handlers in IIS by navigating to your website and then click 'Handler Mappings'. By default these are inherited from the .Net base web.config which is in a different location depending on your .Net framework version.
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config
If a file being requested isn't already explicitly mapped to another handler it falls to a catch all handler (*) as the last option (System.Web.DefaultHttpHandler) which determines if it is a static file or a directory browsing request. So Static files are simply files not bound to another handler already. For example you'll see that *.aspx is already mapped to System.Web.UI.PageHandlerFactory prior to this default handler. So its going to be processed by that handler and not be considered a static file. If you removed that mapping you could technically serve *.aspx as a static file if you really wanted to (just for proof of how it works).
But you can also explicitly list a file type as a static file by adding an entry in your web.config's httpHandlers section mapping the file extensions to System.Web.StaticFileHandler in IIS. For example:
<configuration>
<system.webServer>
<handlers>
<add name="StaticHandler" verb="*" path="*.zip" type="System.Web.StaticFileHandler" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
This example is using the <system.webServer> config section, so its for an App Pool running in Integrated Mode.
Second Question
What about the scenario where a page has been declared with <%#
OutputCache header(without location) . does the images,css,js src
files inside of it , are also being output cached with the same
properties?
No. Because the page is being server as a separate request (maybe even by a separate handler) it can have totally different cache headers/hints. The host page and the resources it may use are not related from a caching perspective.
In fact you may even want to have a shorter cache period for *.html and a longer cache period for *.jpg or *.png? Something to consider.
Third Question
As a best prcatice , I should set one year into the future as the
maximum expiration time.I should use that as the default for all
static content on the site
Hmm... I might not go as far as one year. How about one month? I would set a global policy like this:
<configuration>
<system.webServer>
<staticContent>
<!-- Set expire headers to 30 days for static content-->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
This is the same as the sample you showed above, but is not inside a <location> element, instead it is right in the root <configuration> element so it is the default policy. Again this is for an App Pool running in Integrated Mode. Sometimes you also need to turn on:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<!-- stuff -->
</modules>
</system.webServer>
<system.webServer>
This just makes sure that static files are processed through the managed static file handler which respects the above configuration elements.
Edit to Address Comments
The documentation for the configuration dialog you posted above is located here: Configure the HTTP Expires Response Header (IIS 7)
Apparently these settings are saved in C:\Windows\System32\inetsrv\config\applicationHost.config
I do not have IIS7 and personally develop on IIS 7.5 now. So please post a comment if you can verify this location is accurate!
The static content is the one that IIS is read and send to the browser with out any processing. There you can setup IIS to include some Cache-Control Header to cache it on clients browser computers.
You can do that ether by direct setup IIS, ether by commands on web.config as you say. The commands that you add on web.config and concern the IIS, did not have to do with asp.net it self, but the IIS, and IIS saves his configuration on a different file, so when you change that cache control headers direct on IIS you do not see them on web.config.
Now for the static content like images, CSS, JavaScript, and other similar files they say that you can follow the "never expire" policy by adding 10 years expire.
The issue here is that if you can not change the content of the static file, if for example you cache a javascript file with 10 years, and you make a small change on it, then you need ether to change the file name, ether to add some parameter at the end of it.
Now the <%# OutputCache on a control is referred to the server cache and not to the client, and what is actually do is to cache the render of the control on the server so the next time you ask it to not lose time to renders it again but read it from cache - is still send it to the browser.
And you can also read this answer for some more: What are difference between IIS (Dynamic and Static) cache,OutPutCache and browser cache

Severe Session issues ASP.NET

I have a kind of an ugly situation.
I have a big program that uses session to carry over data from one page to another in a CRM system build in ASP.NET 3.5 C#
The problem is that if you have two instance of this program open in the same browser and browse the same page, the sessions of course gets overridden.
As you can imagine, this is a huge issue and a huge liability for the system.
What is the right thing to do here? I use tons of AJAX, and need to pass objects from page to page, so url parameters is not really an option here.
Any suggestions?
What is your web.config sessionState configured? I think in your situation you can reduce the severity of your problem by configuring it as follows:
<configuration>
<system.web>
<sessionState mode="InProc" cookieless="true" timeout="20"/>
OR
<sessionState cookieless="true" regenerateExpiredSessionId="true" timeout="20" />
</system.web>
</configuration>
But the latter is going to mangle your URLs. You'll end up with ASP.NET inserting session IDs into your URLs, something like http://www.example.com/(S(lit3py55t21z5v55vlm25s55))/orderform.aspx. More about it here.

How do I get ASP.Net CompositeScript to cache in the browser?

I have been trying to improve the performance of a web page that references several separate JavaScript files. I found the new CompositeScript feature in .Net 3.5 SP1 that allows you to combine scripts at runtime through the ScriptManager and it works great to reduce the number of hits on our server at page load.
But the problem is that the combined script never caches on the client side. From everything I've read so far, it should do that automatically. I don't have anything set to prevent it from caching (other scripts we reference that could not be combined cache fine, as do the ScriptResource.axd files). I even added the following to explicitly enable caching in the web.config:
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="true" enableCaching="true"/>
</scripting>
</system.web.extensions>
But it doesn't seem to have an effect.
Has anyone seen this problem or have some suggestions on things I can try to fix it?
Is debug mode on anywhere? Make sure your webserver's Machine.Config has Retail="True"
<configuration>
<system.web>
<deployment retail="true"/>
</system.web>
</configuration>

Resources