<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged medium-fast</title>
<link>https://coderlegion.com/tag/medium-fast</link>
<description>Powered by Question2Answer</description>
<item>
<title>How to Write to Web.config in Medium Trust</title>
<link>https://coderlegion.com/29473/how-to-write-to-web-config-in-medium-trust</link>
<description>&lt;p&gt;If you’re working with an older ASP.NET application running under Medium Trust, you may run into a common problem: your application can read Web.config, but attempting to modify it at runtime fails with a security or permission exception.&lt;/p&gt;
&lt;p&gt;The important point is that Medium Trust is designed to restrict applications from modifying configuration files. Simply using Server.MapPath() and File.WriteAllText() is not a reliable solution—and in a hosted environment, it will generally be blocked by filesystem permissions or the trust policy.&lt;/p&gt;
&lt;p&gt;What can you do?&lt;/p&gt;
&lt;p&gt;If you need to change configuration dynamically, consider these approaches:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Store dynamic settings outside Web.config&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of modifying Web.config, use a data store such as a database or another configuration mechanism that your application is allowed to write to.&lt;/p&gt;
&lt;p&gt;// Example: store application settings in a database&lt;br&gt;
string value = GetSettingFromDatabase(&quot;MySetting&quot;);&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use an application-level configuration file&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For settings that need to be writable, keep them in a separate file, provided your hosting environment allows your application to create and modify that file.&lt;/p&gt;
&lt;p&gt;string path = Server.MapPath(&quot;~/App_Data/settings.xml&quot;);&lt;/p&gt;
&lt;p&gt;File.WriteAllText(path, &quot;Value&quot;);&lt;/p&gt;
&lt;p&gt;App_Data is commonly used for application data because it isn't intended to be served directly to clients.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Change Web.config during deployment&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If the configuration really belongs in Web.config, the safest approach is usually to modify it as part of your deployment process rather than from application code.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;

  
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Your deployment process can generate or transform the appropriate configuration for each environment.&lt;/p&gt;
&lt;p&gt;What about ConfigurationManager?&lt;/p&gt;
&lt;p&gt;You can read configuration values normally:&lt;/p&gt;
&lt;p&gt;string value =&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ConfigurationManager.AppSettings[&quot;MySetting&quot;];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But being able to read configuration doesn't mean the application has permission to write the physical Web.config file.&lt;/p&gt;
&lt;p&gt;Bottom line&lt;/p&gt;
&lt;p&gt;Under Medium Trust, don't design your application around runtime modification of Web.config. If a value needs to change while the application is running, keep that value in a writable data store or separate configuration file. If it belongs in Web.config, update it during deployment.&lt;/p&gt;
&lt;p&gt;This approach is more secure, more predictable, and works better with hosted ASP.NET environments.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/29473/how-to-write-to-web-config-in-medium-trust</guid>
<pubDate>Fri, 02 Oct 2026 03:37:53 +0000</pubDate>
</item>
</channel>
</rss>