~/Web.config read from project root but written to ...\Common7\IDE\~\Web.config ??

 Hey all,

I'm trying to edit my Web.config in runtime, but I get some weird behaviour. Probably something simple I couldn't find on the net. But I'm new to this, so bear with me.

In my code I open the ~/Web.config file and it reads it from the rood of my project folder. When I try to save it to ~/Web.config, it writes it to C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\~\Web.config instead.
How can the same relative path point to 2 places? Read from here but write to there ?!? How do I solve this?

Here is my sample code:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="webconfig._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<hr />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="save Web.Config" /><br />
<asp:TextBox ID="TextBox3" runat="server" Width="754px"></asp:TextBox></div>
</form>
</body>
</html>
 


Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webconfig
{
public partial class _Default : System.Web.UI.Page
{
System.Configuration.Configuration config;

protected void Page_Load(object sender, EventArgs e)
{
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
TextBox1.Text = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
}

protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"] = TextBox2.Text;
config.SaveAs("~/Web.config");
TextBox3.Text = config.FilePath;
}
}
}
 

Web.config 
<configuration>

<appSettings>
<add key="myKey" value="ABCD"/>
</appSettings>
<connectionStrings/>

<system.web>
<compilation debug="true" />
<authentication mode="Windows" />
</system.web>

</configuration>
  
0
Dieko
3/8/2008 10:03:36 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

7 Replies
1076 Views

Similar Articles

[PageSpeed] 26
Get it on Google Play
Get it on Apple App Store

Use Server.MapPath to get the physical location of your file and use it in the save. 


Hosam Kamel

Remember to click on Mark as answer on the post that helped you
0
HosamKamel
3/8/2008 11:00:06 AM

Configuration c = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration (Request.Path);


Mark replies as answers if they helped you solve the problem.
0
siva_sm
3/8/2008 11:11:52 AM

hi, please check this out,

http://www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-Time.aspx 


Bryian Tan
MCP, MCAD
0
BryianTan
3/8/2008 2:35:20 PM

Dieko:



protected void Page_Load(object sender, EventArgs e)
{
config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
TextBox1.Text = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
}

protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"] = TextBox2.Text;
config.SaveAs("~/Web.config");
TextBox3.Text = config.FilePath;
}
}
}

 

 

 

 

Try like this

protected void Page_Load(object sender, EventArgs e)
{

 

config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

 TextBox1.Text = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
}

protected void Button1_Click(object sender, EventArgs e)
{
 
AppSettingsSection objAppsettings =      (AppSettingsSection)objConfig.GetSection("appSettings");
 objAppsettings.Settings["myKey"].Value =TextBox2.Text;      
objConfig.Save();
TextBox3.Text = config.FilePath; 
 

visit my blog for more http://krishnaprasadaccot.blogspot.com/2007/10/introduction-asp.html 


Krishna Accottillam or Akkottillam

MCTS
0
krishnaprasadaccot
3/8/2008 6:42:06 PM

try this, tested and works

   protected void Page_Load(object sender, EventArgs e)
    {
       //Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@"Web.config");
        TextBox1.Text = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@"~");
        config.AppSettings.Settings["myKey"].Value = TextBox2.Text;
      //  System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"] = TextBox2.Text;
        config.Save();
       // config.SaveAs("Web.config");
       TextBox3.Text = config.FilePath;
    }
  
Bryian Tan
MCP, MCAD
0
BryianTan
3/9/2008 1:26:15 AM

wow!! Thanks for all the replies. I tried them all but I think the best solution to the problem in my situation is Bryian's. Still have to test it on the production server though. But shouldn't be an issue.

0
Dieko
3/10/2008 9:18:50 AM

they might be a problem in the production server, if that happen try to add

<identity impersonate="true" /> in the web.config file 


Bryian Tan
MCP, MCAD
0
BryianTan
3/14/2008 6:15:28 PM
Reply:

Similar Artilces:

running 2 web.configs in the same website 1 in /web.config and one in /swf/web.config
Im having issues doing this. and when i remove the authentication from the swf/web.config it still doesnt seem to be applying to the child website at all. Whats the proper way to set a child website? Thanks!!  The lower most web.config overrides all previous settings. So if you remove the section from the /swf/web.config whatever settings you have in the web.config in the next higher level will have an effect on the content of the child folder.So instead of removing a section, try giving appropriate settings in the /swf/web.config....

Web Deployment Project - Config Properties
I'm using a Web Deployment Project to compile my web site. In the Property Pages - Configuration Properties - Deployment section, there is an option to 'Enable Web.config file section replacement'. I have this working to replace certain sections of my web.config for each build config (release/staging/dev). I think this makes sense so other developers do not have to remember to change the web.config file. HOWEVER, this only seems to work when replacing parts of web.config; such as appSettings, connectionString, etc. This would require me to have separate config files for each section and bui...

web.config file of root directoy interferes with the sub-application web.config.
Hello! I encountered a problems when I added new sub-application to my application,  with its own web.config, When I try to open page www.mypage.com/subapplication/ It reads the web.config of www.mypage.com and throws an exception (I think it tries to loadsome dlls from root-bin directory (exactly ATLAS files..)  <add name="ScriptModule" type="Microsoft.Web.Services.ScriptModule"/> - exception on this line ) But why it reads the web.config of root dir? What can I do to solve that problem? Thank you.Artashes This is the expected behavior. web.config are inher...

web.config in Root Web
   On my server (Windows 2003 Sever) my root web (C:\inetpub\wwwroot) is an Atlas enabled site.  Everything there works as it should.  However when I view a published subweb (c:\inetpub\wwwroot\subweb1) which is not Atlas enabled I get the following error: Could not load type 'Mcrosoft.Web.Services.ScriptModule'.  The error message indicates that the error is in the root web web.config, not the subweb web.config. My question is this; If my root is Atlas enabled must all my subwebs be also?  If not, how do I get around this error?  Thanks for any help...

VS 2005 SP1 + .NET AJAX => automatically adds web.config in nested Web Application Project when opening project or solution
I get a 'Configuration Error' on an aspx page because VS added automatically a web.config to my nested Web Application Project where no web.config file should be. This effect I have noticed since I have installed SP1 and .NET AJAX Extensions. Why is VS 2005 adding the web.config file and how can I disable this behavior? Following is the content of the automatically added web.config file (comments omitted): <?xml version="1.0"?><configuration>    <appSettings/>    <connectionStrings/>    <system.web> &...

Read info from web.config, machine.config, app.config....
I know how to read info from the web.config, see code:  Protected path As String = "/Framework" Protected config As Configuration = WebConfigurationManager.OpenWebConfiguration(path) How can I read also the data from machine.config and other config files, like the app.configs?Johan TheunissenMCPD, MCSE, MCTS BizTalk 2006==============================Please mark the most helpful reply/replies as "Answer". ...

how to let the web.config in the sub-directory to NOT to inherited from Root's web.config.
Hi,all,   How to let the sub-directory to avoid the authentication control from Root's webconfig? I heard that we can add a new web.config to the sub-directory. And then we can slove the problem.   Virtual directory is :http://localhost/main   Sub-directory is : http://localhost/main/reminder   My application used the Forms Authentication, then all files in every directory is controlled by the Forms Authentication. But now i want a sub-directory to avoid this. (user can access this directory directly, Needn't to Login.)   In my application, the user can ac...

Web Site Project: How to configure web service URL in web.config (web service defined in a referenced DLL)
I have a web site that references a business logic DLL project. In the business logic project, there is a web service defined in its app.config file something like this: <applicationSettings>  <MyNamespace.BLL.Properties.Settings>    <setting name="MyNamespace_BLL_MyServer_ServiceName" serializeAs="String">    <value>http://blah.blah.blah.asmx</value>  </setting>  </MyNamespace.BLL.Properties.Settings></applicationSettings> When the web site is published, the web ...

Need to put this in a web.config file. Transfer a collection from app.config to web.config.
<!--<setting name="Groups" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>GL CA Corporate Data</string> <string>GL CONT Contractors</string> <string>GL IT SolutionDelivery</string> </ArrayOfString> </value> </setting>-->   I need to put this in the below web.config.<?xml version="1.0"?><!-- Note: As an alternative to hand editing this fi...

Merging my web.config.mylibrary with the web.config
I see multiples web.config files in the framework 2.0 folder.  There is a way to had a web.config.mylibrary file that contains part of the configuration section.For example, I want that the profile section to be in the web.config.mylibrary file and when the web site will be load, the two web.config will be merge.  Is it possible to such thing ? Hi Biz, It is possible to-do this using the configSource property on configuration section elements.  You could then store different configuration sections in different configuration files in a distributed way.Here is a MSD...

Web Deployment Project / Setup Project and editable web.config
I'm working on an installer for a website. The Installer will prompt the user for the web settings such as as various AppSettings. How would you recommend implementing this? Below I describe my initial thoughts but they seem over complicated !!!! My first idea is to created the Web Deployment Project and create a web.config section overwrite file, and then using it in the usual manner to overwrite the web.config appSettings section at installation time.Property Pages->Deployment section to "appsettings=appsettings.config"Then to make this appsettings.config file editable by a Custom ...

Web Deployment Projects: Web.config sections <system.web>
I like "Web Deployment Projects" a lot - WELL done!I encounter the following by trying to replace the system.web config section in Web.config.During the build I get   Web.config (1): error WDP00002: missing section system.web.I can replace other parts of the Web.config, like appSettings, connectionStrings and even system.runtime.remoting, but not system.web.Any ideas? Thankx! I think you have the honors of finding the first bug.  We're using Configuration.Sections[] to acess the replaceable sections and it doesn't appear to work with system.web.I'll enter the bug. ...

How do I merge web services' app.config with web app's web.config? Help!!
I have a web application project that uses a separate class library containing web service references (service layer). I want to be able to configure the web reference URLs dynamically and I know you can set this up by setting the URL behavior to  'Dynamic', etc, etc. The problem is VS2005 puts the URLs in the project's app.config file. When the class library becomes a DLL component, I have no way of accessing the app.config file.Is there a way to make the web reference project to look for the URLs in the web.config instead of its app.config? Or is there a way to merge this app.config w...

Should local Web.config overwrite Production Web.config
I have a hosting service that supporst ASP.net 2.0.  I have an SQL Server 2005 Express account with them.  I have just started using them and have written my first web app. I have read quite a few things but it is not clear to me whether or not when I copy my web site (using VWD) if I should copy the web.conf file or not.  My local app runs fine but after copying everything to my production web site, I am not able to access the production web site in my browser.  I suppose it may have something to do with the question I've asked concerning Web.config. Is there a partic...

Web resources about - ~/Web.config read from project root but written to ...\Common7\IDE\~\Web.config ?? - asp.net.getting-started

Project - Wikipedia, the free encyclopedia
For the urban low-income housing buildings called projects, see public housing . For other uses, see Project (disambiguation) . The word project ...

The Project Shrink - Welcome To Shrinkonia.
... about the importance of the environment we work in and the stories we tell, we should have a Set Designer and a Storyteller on our projects. ...

Better Projects
I think this book by Tobias is beautifully written. There is real craftmanship in the prose here. The book is made up of independent self contained ...

WestConnex project to acquire more of Sydney Park
More slices of the award-winning inner-city Sydney Park will be carved off to make way for the WestConnex motorway and surrounding roads.

The Guilt Project.
From "Feminist Resolutions for 2016" by Ann Friedman in New York Magazine: Start the Guilt Project. Thanks to your holiday binge-watch of Making ...

50 employees left Zappos before an important project was finished
... group of engineers and technical types helping the company move to Amazon's cloud computing infrastructure in the so-called "Super Cloud" project, ...

Large Battery Storage Project Teams With Solar Farm In UK
Originally published on Sustainnovate. By Henry Lindon The UK will soon be getting a new industrial-scale battery storage project, one of its ...

Activision Blizzard Sets Stacey Sher To Spearhead Film, TV Projects Including ‘Call Of Duty,’ ‘Skylander,’ ...
Video game maker Activision Blizzard has set producer Stacey Sher to team with former Disney exec Nick van Dyk to run Activision Blizzard Studios ...

Project X Zone 2 is getting a 3DS demo later this month
Did the holidays completely warp your sense of time? Because Project X Zone 2 is coming out next month internationally. And before it's out, ...

Google wants automakers to help its self-driving car project
... some help from traditional automakers. Speaking at the Automotive News World Congress, John Krafcik, the head of Google's autonomous car project ...

Resources last updated: 1/17/2016 1:34:12 PM