ten5peed
ten5peed

Reputation: 15900

Custom app_offline.htm file during publish

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?

Upvotes: 35

Views: 24550

Answers (10)

Walkman
Walkman

Reputation: 119

2024 and this is still not possible in Visual Studio 2022... VS's local copy of app_offline.htm seems to no longer exist.

I managed to get this to work fairly simply. A lot of the comments here and in the linked Github Issues (#3425 and #6692) miss out on some parts or the tools have changed since. The biggest help to me figuring this out was kirkone's "VSTS DNX Tasks" repo with this script: PublishWebPackage.ps1

Note that I'm using VS 2022, .Net Core 6 (TFM net6.0), SDK Microsoft.NET.Sdk.Web and publishing from within VS - Build->Publish to Azure - if you're publishing to some other system then you will need to adapt the remove script. This will probably work with the dotnet publish system but I haven't tested.

  1. add your preferred app_offline.htm file to your project's root.
  2. create a file called RemovePublishedAppOffline.ps1 in your project with the following contents to remove the offline file after publish is complete, replacing placeholder text with correct values:
# PowerShell script to delete app_offline.htm from WebApp after deploy
# Set deploy password here:
$password = ''

$vfsApiUri = "https://<YOUR SITE>.scm.azurewebsites.net/api/vfs/site/wwwroot"
$username = '<USERNAME>'
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$authHeader = @{Authorization=("Basic {0}" -f $base64Auth);"If-Match"="*"}

Invoke-RestMethod -Uri "$vfsApiUri/app_offline.htm" -Headers $authHeader -UserAgent "powershell/1.0" -Method DELETE -TimeoutSec 600

To get the deployment username & password, find your app in Azure and go to Deployment Center: Deployment Center in Azure For the vfsApiUri, go to Advanced Tools: Advanced Tools in Azure Then copy the link labeled Files:
link to Files in Kudu

There is a wiki page documenting how to get to this system (Kudu) if it ever changes in future: https://github.com/projectkudu/kudu/wiki/Accessing-the-kudu-service

  1. add the following sections to your *.csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">

[...]

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <None Update="app_offline.htm" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

  <Target Name="RemoveOfflineAfterPublish" AfterTargets="AfterPublish">
    <Exec Command="powershell .\RemovePublishedAppOffline.ps1" WorkingDirectory="$(RunWorkingDirectory)" />
  </Target>

</Project>

The first itemgroup copies the offline file to the output directory on building for release, which VS pushes to the WebApp first. The second item runs the powershell script to remove the offline file when publish is complete.

When you deploy your app, the following should be output at the end of the deploy log:

[...]
Updating file (<filename>).
Updating file (<filename>).
Publish Succeeded.
powershell .\RemovePublishedAppOffline.ps1

Web App was published successfully https://<YOUR SITE>.azurewebsites.net/

Upvotes: 0

joe
joe

Reputation: 51

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.

Upvotes: 5

jscarle
jscarle

Reputation: 1315

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"

Upvotes: 5

Lemonseed
Lemonseed

Reputation: 1732

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.

Upvotes: 17

Joop Stringer
Joop Stringer

Reputation: 51

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.

Upvotes: 1

Jay
Jay

Reputation: 3355

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.

Upvotes: 3

Eonasdan
Eonasdan

Reputation: 7763

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.

Upvotes: 28

Andrew Bullock
Andrew Bullock

Reputation: 37436

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.

Upvotes: 10

griegs
griegs

Reputation: 22770

Have you seen this?

App_Offline.htm

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

Upvotes: -3

user164226
user164226

Reputation:

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.

Upvotes: 0

Related Questions