Friday, February 20, 2009

Programatically changing Web.Config

I have this cool thing I made that needs a HttpHandler to process some commands. Therefore, in the code, I am trying to change the web.config whenever possible. I do it in the usual way:
... .Save();
. That should work fine, unless you don't have writing rights on the web.config file. But you don't need only that! I gave rights to the web.config file, but I got this silly error: "changing web.config An error occurred loading a configuration file: Access to the path 'bla...bla...bla...bla.tmp' is denied.".

In other words, in order to write to the web.config you also need rights to add files to the web folder in order to create temporary files. Hmm, I thought the temporary files folder was used for that... :-|


Lex Y. Li said...

I don't recommend this approach because when you modify web.config, ASP.NET runtime will be notified and the worker process who serves this web application will be recycled. Some people may not want this effect sometimes. :)

Siderite said...

I agree that is an undesirable effect, but the alternative is to show an ugly error with instructions for the user to do what the control itself could do.

Besides, this would only happen when first using the control (thus in the development/testing phase).