32

Is it possible to edit web.config file of my cloud app deployed on Windows Azure without redeploying my app ?

Scenario is like-->

  1. Cloud app is deployed on Azure with 3 instances.
  2. web.config has some static text in appsetting which is displyed on the Home Page(for example - © 2009 My site. All rights reserved)
  3. Now I wish to change that static text mentioned in Web.Config from 2009 to 2010.
  4. Now I wish to edit Web.Config without redeploying my site.
  5. I do not wish to --> deploy my app on stagging with updated Web.Config and then swap it with production.

Is there any trick to update the files from package deployed at runtime ?

Kushal Waikar
  • 2,976
  • 5
  • 26
  • 31

5 Answers5

20

According Maxim in the comments below this answer is now out of date.

You can programmatically modify the web.config settings of a web role in the OnStart event using the Microsoft.Web.Administration.ServerManager library.

*** leaving the original answer as it was correct at the time and as, I have not used Azure since answering this question, and I am not 100% sure of the valid answer.

In a word no.

You must use the service configuration file for such settings.

To decide on whether to place keys in the service configuration settings versus web configuration settings.

You could ask yourself the following questions:

Does this setting change with every deployment? If so then the web configuration settings is the correct place for this information.

Will this setting change after deployment? If so then the service configuration settings is the correct place for this information.

The Web.config file is part of the deployment package and so is read-only when deployed to Azure, in order to update the settings you will need to redeploy.

Whereas the service configuration file is uploaded with, but not packaged with, the deployment package, and therefore you can upload or edit the file without redeploying your service.

Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • Thanks a lot Nicholas for such a nice answer. – Kushal Waikar Mar 03 '10 at 07:00
  • 1
    Actually it is possible to change the web.config file of a cloud service. Just enable remote desktop, remote in, and change the file for all three instances. – Matt Jun 25 '13 at 14:02
  • This answer is out of date. You don't even need to do that. You can programmatically modify the web.config settings of a web role in the OnStart event using the Microsoft.Web.Administration.ServerManager library. – Maxim Gershkovich Jul 31 '13 at 15:08
  • Maxim, I will change my answer to include your comment. – Nicholas Murray Jul 31 '13 at 15:31
  • @Matt Yes, you can use RDP to manually edit your web.config, but be aware that if your cloud service gets redeployed (and from experience Azure randomly does this) you lose your changes. – Dunc May 08 '15 at 11:56
  • As of VS.NET 2015 you can right click on any file/folder in your solution and publish individually to your Azure App Service... – AgonyOfVictory Nov 15 '16 at 18:36
8

It is possible and if you do changes to web.config, the ASP.NET app will restart with the new settings, just like you would expect on "normal ASP.NET". You just need to be certain you are making the right updates to web.config or your instance is probably impossible to repair once ASP.NET is gone with a web.config error.

If you have web.config as part of your deployment package you need to change the file permissions on it, which you can do from RoleEntryPoint.OnStart().

Another way would be to have your ASP.NET app write the initial web.config itself via some init web handler.

By default the ASP.NET app will not have update or delete permissions to files from the deployment package or files written by your RoleEntryPoint code, while it has full access to files it itself create. This behavior is only experienced on "real Azure" while things behave otherwise when running locally with the SDK.

Check this for info about writing files and setting permissions on files from RoleEntryPoint.OnStart(): How can I get the WebRole site root path from RoleEntryPoint.OnStart()?

Community
  • 1
  • 1
mawtex
  • 1,564
  • 1
  • 12
  • 21
1

Yes, this can be done. The steps are:

  1. Use the environment variable 'RdRoleRoot' to find the root of the role.
  2. Look for 'RoleModel.xml' file
  3. Load the 'RoleModel.xml' file and find the entry for your site under "Sites" node
  4. In this entry look for 'physicalDirectory' attribute which is the relative path to the directory containing your web.config file
  5. Combine role root path with this physicalDirectory path to get the full path

I wrote a blog post http://anuchandy.blogspot.com/2014/02/changing-webconfig-file-deployed-on.html with sample code.

0

yes you can, all you need to do is create a RDC for Azure and goto E:\siteroot\0 where you will see all your deployed files. and just edit web.config what you like. it will work for both Production and staging no need of redeploy or re-image or reboot.

May this post Thread is asked long back, but for some one like me this definitely helpful :-)

Ramakrishna
  • 4,928
  • 3
  • 20
  • 24
  • 1
    But in the event of an automatic redeployment (by Azure infrastructure) such changes will be lost I think. You should make the changes at the source, i.e. in your local code, and then redeploy. This will help you to keep things consistent. Not to mention the fact that you are advicing to change production configuration without a chance to test first. – Werner Jun 20 '14 at 10:34
  • @werner I suggested this because if we forget to change DB name of Azure at that time we can directly change, in that case re-deployment is not that necessary. – Ramakrishna Jul 25 '14 at 07:26
  • This is just wrong! Changes are wiped out when VM is rebooted/re-imaged during which Azure will create the instance from the original package. This only leads to unexpected down times than solving the issue. So in my opinion, this *MUST NOT BE DONE* in a PROD environment. – Illuminati Dec 14 '14 at 22:17
  • When your solution is out of date showing thousands of errors, your project needs changes as SDK has changed, and you only want to change Web.config, this is a valid answer! – Daniel Skowroński Feb 12 '15 at 09:43
  • Yes thats true, when I edit my web.config file and past any code and save the file and again on refresh pasted code disappeared? any logic, site is connected with automatic deployment pipeline. – Saad Awan Sep 28 '20 at 12:26
0

13th July 2018

The Azure Portal has an App Service Editor (in preview) under your deployed App Service. Selecting this gives a Visual Studio Code-like editor pointed at your deployed Azure Website making edits to web.config (or any other file) easy:

Azure App Service Editor screenshot

Community
  • 1
  • 1
Andrew
  • 12,991
  • 15
  • 55
  • 85
  • when i edit any thing in web.config file and saved the file and after that restarted or refresh the azure app, the changes get disappeared. site is conected with automatic azure deployment pipeline – Saad Awan Sep 28 '20 at 13:07