34

When I publish my ASP.NET MVC application it generates a app_offline.htm file to take the site offline while it updates the website and then deletes the file once the publish is successful.

This is cool and I really like the idea, but I want to create my own custom app_offline.htm file that the publish action is aware of and put it somewhere where it doesn't effect my development site - i.e. it doesn't sit in the root of my development site rendering it offline all the time.

EDIT: From the comments on Scott Gu's post about app_offline.htm, it seems that customization of the app_offline.htm file wasn't possible with VS 2005 - has this changed with VS 2008 and now VS 2010?

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Charlino
  • 15,802
  • 3
  • 58
  • 74
  • "From the comments on Scott Gu's post about app_offline.htm" doesn't link as expected, as I'm not seeing anything about this on the page. – James Skemp Sep 22 '10 at 22:49
  • Don't know why it's not linking but if you have a look at a comment made by Scott Gu in reply to Rich which he made on Friday, December 01, 2006 at 12:56 AM - you'll see what I'm talking about. – Charlino Sep 23 '10 at 06:03
  • I would love to know the answer to this, I want to do the exact same thing. It seems it would be really easy for Microsoft to implement in a future release and it would actually be super handy and practical. – Aaron Oct 25 '10 at 15:12

9 Answers9

28

I know this is old but since I found a solution after looking here I thought I should provide an answer. VS 11 holds the publishing app_offline.htm file in this location:

C:\Users\[user]\AppData\Roaming\Microsoft\VisualStudio\11.0\app_offline.htm

I have tested this and customized it and it does work if you change this file. The down side, of course, is that this is the file used for all web publishing.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
  • +1 Thanks for answering... it's not an idea solution but will suit many for the time being. I just hope the Visual Studio team builds something in, seems like a nice feature to me :-) – Charlino Nov 18 '11 at 17:31
  • 3
    unfortunately, I just found out VS decided to override it some time later. I agree, it would be nice to have a customizable solution – Eonasdan Nov 18 '11 at 18:41
  • @Eonasdan Do you know what causes the overwrite? Update to VS possibly? – FarFigNewton May 17 '13 at 15:23
  • @guanome I don't think it was a VS update. I haven't messed with this in a while, but I don't think it lasted longer then one or two publishes. – Eonasdan May 17 '13 at 21:16
  • 2
    I just changed the C:\users\[user]\AppData\...\app_offline.htm to read-only on my local computer and it seems to be holding up. – Steven Feb 06 '14 at 15:12
  • 1
    VS2013 seems to ignore this file altogeather – Tod Mar 05 '15 at 14:17
  • I tried to modify app_offline.htm in C:\Users\User\AppData\Roaming\Microsoft\VisualStudio\14.0 and publish my application. But during publish, there is no app_offline.htm on the page. I also tried with the original app_offline.htm but the result is the same. Any idea? – Jack May 20 '16 at 09:13
  • There is an issue about app_offline.htm customization support - https://github.com/aspnet/Tooling/issues/530 – resnyanskiy Nov 16 '16 at 05:54
  • Also using VS2k17 (15.0) and it ignores the file on disk and I read that it uses some hard coded string. – sobelito Jan 20 '18 at 00:20
17

An easy solution that many might find suitable involves simply adding your own file also named "app_offline.htm" to your solution.

I have tested this method, using Visual Studio 2015, and it does indeed work.

However, the only drawback is that during the publishing process, the default generated app_offline.htm file is copied first, then the solution files in what appears to be in/near alphabetical order.

This means that your custom app_offline.htm file quickly (but not instantly) overwrites the system generated file. Therefore the (ugly) system generated copy of app_offline.htm might be served to requests within a 1-2 second window of initiating the publish, before being updated with your custom file.

The publishing process automatically deletes the app_offline.htm from the remote server irrespective of its content or origin.

The advantage is (over replacing the system copy) is that your own copy is portable, is automatically solution-specific, and works with source control.

I know this question is old, but I hope this helps others coping with this issue.

Lemonseed
  • 1,644
  • 1
  • 15
  • 29
10

I use my own

app_offline.htm_

file in the solution, which gets published. My deployment script then renames it (removing the trailing _) to make it active.

I can then run my db scripts/do whatever then rename the file bringing the site back.

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
  • How does your deployment script remove the trailing slash? Is the renaming of the file in the last step a manual thing? – Charlino May 18 '10 at 21:24
  • i simply make app_offline.htm as i normally would (simple html/css) and add this to my project (but called .htm_ so it doesnt take my dev site offline when debugging). make sure you set the build type to content. i publish and upload the code, then i have a deployment script which copies the code over, and renames `app_offline.htm_` to `app_offline.htm`. simples – Andrew Bullock May 19 '10 at 09:24
  • 1
    Does you deployment script then delete the file once it's done? – Charlino May 20 '10 at 04:26
  • no, i leave it there so when i roll out new code, i can turn it back on – Andrew Bullock Feb 25 '11 at 11:07
  • 1
    @AndrewBullock Why did not post your code instead of explaining everything by words??? – Jack May 20 '16 at 09:07
  • @AndrewBullock I tried to modify app_offline.htm in C:\Users\User\AppData\Roaming\Microsoft\VisualStudio\14.0 and publish my application. But during publish, there is no app_offline.htm on the page. I also tried with the original app_offline.htm but the result is the same. Any idea? – Jack May 20 '16 at 09:13
  • @AndrewBullock I have 2 question, First one is where we should add `app_offline.htm_`? solution root or project root? And the second question is what is your script for removing the trailing _ and then and visa versa? – user197508 Jul 05 '18 at 16:51
5

Andrew Bullock's answer works like a charm! although it depends on which visual studio you are using.

C:\Users\USERNAME\AppData\Roaming\Microsoft\VisualStudio

is the directory you should first land at, choose your VS version[ 8.0/9.0.....] and edit the app_offile.htm file.

InteXX
  • 6,135
  • 6
  • 43
  • 80
joe
  • 51
  • 1
  • 1
5

The original contents of app_offline.htm are indeed stored in a mystery location (probably hard-coded inside of one of the binaries), however Visual Studio does write the contents to a physical file before uploading it.

If you've published at least once, a simple search in a command prompt using dir C:\app_offline.htm /s/a/b should come up with the location where the temporary file is written. It should be something like C:\Users\username\AppData\Roaming\Microsoft\VisualStudio\16.0_5fc0d832\app_offline.htm. I'm not sure if that hexidecimal string at the end is the same for every installation or user.

Setting that file to read-only does indeed prevent Visual Studio from overwriting its content, and modifications to that file will in fact be uploaded during publishing. However, it causes an internal error that can eventually prevent it from uploading over time.

The app_offline.htm is written before the post-build event, so it just needs to be overwritten before Visual Studio starts uploading.

A more resilient approach is to do the following:

1) Create a custom app_offline.htm in your project. (Not in the root folder of your project, it will prevent you from using F5 Start Debugging.)

2) Use the post-build event to copy the custom app_offline.htm from your project folder to Visual Studio's staging folder.

Post-build event command line:

copy /y "$(ProjectDir)Resources\app_offline.htm" 
"C:\Users\%username%\AppData\Roaming\Microsoft\VisualStudio\16.0_5fc0d832\app_offline.htm"
jscarle
  • 1,045
  • 9
  • 17
  • I think, this solution seems as the best one for whomever want to automatize custom appoffline deployment and not touch anything manually. – burcinsahin Jan 19 '22 at 18:19
  • This is the only answer that makes it possible to have different app_offline.htms for each website you maintain. – mm201 Nov 01 '22 at 19:18
3

You can use a batch file which calls the compiler with the current directory information as parameters... It then copies app_offline.htm and copies the new file over. Run it from where the source is.

I personally use a program which is just a bit more elaborate then the batch and also checks hash's of files to determine if they need updating makes a zip of new files and extracts it to the remote host and eliminates files we designate are for unit testing when going from test to production. I have also combined SVN integration into my solution so when you publish for test or production you also optionally commit to SVN. The program is stored on a network drive and is called from a batch file on the local PC with the current directory info. This way dev's dont have to update to the new deployer or the app_offline if any changes occur. Finally it removes the app_offline.

Check out http://msdn.microsoft.com/en-us/library/system.web.compilation(v=vs.80).aspx for more info or http://msdn.microsoft.com/en-us/library/ms229863(v=vs.80).aspx if you are just making a quick batch!

Additionally in the post you initially referenced and I have also verified you can actually change it but you change it for others using the publish feature as well. This is why you were given the answer you were.

Jay
  • 3,276
  • 1
  • 28
  • 38
1

For users of Visual Studio Express 2013 for the Web, the file is located at C:\Users[user]\AppData\Roaming\Microsoft\VWDExpress\12.0

That one can be modified to your needs.

0

Back in 2006, Scott Gu said that there was no way to customize the file which VS generates on Publish.

Comment within original article

I'd be interested in solving this too, but I was unable to turn up anything definitive to the contrary on Google.

-3

Have you seen this?

App_Offline.htm

It might not be (exactly) what you want but it does solve the issue I think.

griegs
  • 22,624
  • 33
  • 128
  • 205