6

I have an ASP.NET master page which references a #include file as follows:

<!--#include virtual="/includes/scripts.inc"-->

I have modified the file /includes/scripts.inc but the changes do not show up in pages. What needs to be done so modifications will be reflected?

I need to avoid the following:

  • restarting the server
  • restarting IIS
  • modifying web.config (doesn't appear to have any effect)
  • pretty much anything that causes the app domain to restart

Any other options? Is there a setting which affects how long IIS caches #include files?

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
frankadelic
  • 20,543
  • 37
  • 111
  • 164
  • 1
    What kind of code do you keep in those includes? I don't think you ought to be using those old-school kind of includes in ASP.NET. – Jeroen Jul 28 '10 at 20:51
  • I knew someone would ask that. Just assume that they're required for the application :) – frankadelic Jul 29 '10 at 03:36
  • My suggestion is to avoid this include style - its coming from asp, maybe for easy migration, but its not handle so perfect by asp.net. Your pages did not understand that they need update, so the don't and stay as they have compiled in the first place. – Aristos Jul 31 '10 at 18:12
  • @frankadelic - A fundamental aspect to your question is whether scripts.inc includes server-side code and if so, in what language (e.g. VBScript?). – Thomas Aug 02 '10 at 00:29
  • Thomas - I actually have two sets of .inc files. Some are just static HTML, others include inline ASP.NET control references. – frankadelic Aug 03 '10 at 02:09
  • @frankadelic - Why are you trying load controls using an include file? If the control that should be loaded is static, then just drop the actual control reference in the page. If the control that should be loaded is dynamic, then load it using code-behind. – Thomas Aug 03 '10 at 14:30
  • @frankadelic - In general, your best bet is to solving the "caching" issue will be to not use #include as others have said. The way it is implemented in .NET was simply not designed for dynamic content like ASP Classic which re-read the file on each request. – Thomas Aug 03 '10 at 14:33
  • @frankadelic - Are you sure you don't have something like output caching enabled? In a test project I built, I am able to change the include file at will and the changes show each time. Granted, my include file has only an asp:button control tag and some markup. – Thomas Aug 03 '10 at 15:36
  • 1
    Is this on your local dev box or has this been deployed to a server? – Conrad Frix Aug 05 '10 at 22:24
  • this is on a deployed server (Windows 2008, IIS7) – frankadelic Aug 06 '10 at 03:43
  • @Thomas I have been getting inconsistent results. On a simple one-page test, the include file refreshes... However, on my dev site with hundreds of pages, the include file does not refresh. Need to investigate this further... – frankadelic Aug 06 '10 at 03:59
  • https://stackoverflow.com/questions/1890055/iis7-including-html-in-aspx-prevent-caching?rq=1 – Tyler S. Loeper Dec 18 '17 at 19:59

4 Answers4

4

First, as you probably know, you shouldn't use the #include directive with ASP.NET. The right solution is to use a user control or server control. However, if what you want is to inject a bunch of HTML and Javascript into a page (i.e. no server-side code), then you could use Response.WriteFile:

<%@ Page Language="vb"%>
<html>
<body>
    <% Response.WriteFile( "scripts.inc" ) %>
</body>
</html>
Thomas
  • 63,911
  • 12
  • 95
  • 141
  • 1
    Can't do inline script. Pages are marked CompilationMode="Never". – frankadelic Aug 06 '10 at 04:03
  • @frankadelic - But isn't the only reason they are marked with CompilationMode=Never in order to try to solve this refresh issue with include files? – Thomas Aug 06 '10 at 04:24
  • No, that's actually a design decision we made in this application. The ASPX files are published from a back-end system, and we have to use CompilationMode="Never" to prevent recompilations... because that will cause the App Domain to restart, HttpRuntime.Cache to be flushed, etc... http://dotnetslackers.com/ASP_NET/re-666_ASP_NET_2_0_No_Compile_Pages.aspx – frankadelic Aug 06 '10 at 16:31
  • @frankadelic - If you use Response.WriteFile, it will not cause a recompilation if the include file does not contain compilable stuff. If the include file does compilable stuff, then using an include file is not the right approach. A user/system control, declared statically or loaded dynamically solves the recompilation issue, will likely be faster and will put to rest any caching issues (server-side at least). – Thomas Aug 06 '10 at 16:42
2

So as long as it's really static include, just load the file in C# code and inject it yourself. then put it in the cache with file dependency and the object will turn null after you change the file which will provide you the flag to read it again.

If it's not really static include (static == no ASP.NET controls - it can be very changeable otherwise - like a spew from a DB or different CSS for very user ) then you want to mess with page compilation and that's not going to happen unless you go and write your own template processor or something :-)

ZXX
  • 4,684
  • 27
  • 35
1

Doens't have anything to do with caching, either server-side or client-side. It's a compilation issue. #include isn't caught as a modification in ASP.NET when changed, so the page isn't rebuilt.

This KB should help: http://support.microsoft.com/kb/306575

Stan
  • 746
  • 1
  • 17
  • 35
  • 1
    Not so sure it has to do with compilation. Example: if I create an .aspx page with header CompilationMode="Never", ASP.NET will update the page if I modify HTML within the aspx... No compilation is even happening, yet the page is updated. ....anyways, the gist of my question is, how to get the changes reflected when I open the page in a browser... whether it be caching, compilation, or some other factor. – frankadelic Aug 03 '10 at 02:07
1

If you're caching files that might need to change, you should include a version number in the file name.

eg: <!--#include virtual="/includes/scripts-1.0.inc"-->

then when you need to make changes to it, update your include to:

<!--#include virtual="/includes/scripts-2.0.inc"-->
Andrew Lewis
  • 5,176
  • 1
  • 26
  • 31
  • this might work, but defeats the purpose of an include file... since I would need to update every page that references the include file. – frankadelic Aug 06 '10 at 03:41