Reputation: 21
I have used following code to change web.config at run time
Configuration conf = WebConfigurationManager.OpenWebConfiguration("~/");
conf.ConnectionStrings.ConnectionStrings["ApplicationServices"].ConnectionString = "Test";
conf.Save();
But I am getting an error - "Failed to map the path '/'." I have searched on internet and worked on all the solutions like 1. Change the path to "/" 2. Change the path to "~/"
but same issue occurred. There is one point I would like to say is my application is not hosted on IIS, but running though visuals studio using asp.net development server
Upvotes: 1
Views: 4835
Reputation: 28387
Replace ~
with HttpContext.Current.Request.ApplicationPath
i.e.
Configuration conf = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath)
~
should work, I suggested ApplicationPath
to be sure of Virtual Directory differences when hosted on IIS instead of VS DevServer.
In fact if you are targetting web.config
in root, then you just need to pass null
as the parameter. No need for passing ~
.
Edit:
Are you working with administrative privilege? For this to work on VS Development Server, you have to start Visual Studio with adminstrator privileges. Right-click Visual Studio and Run As Administrator
.
Upvotes: 1