如何保存配置到web.config文件? [英] How to save configurations into web.config file?

查看:255
本文介绍了如何保存配置到web.config文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Asp.net动态网站应用程序,不能配置保存到web.config文件。

I'm using a Asp.net dynamic web site application and can't save configurations into web.config file.

我已经试过这样的事情,但不起作用!

I have tried something like this, but doesn't work!

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

        configuration.AppSettings.Settings["Value"].Value = "Some value";
        configuration.Save();

错误我已经得到了与

Error I have got with

1. OpenWebConfiguration(Server.MapPath(".")) & OpenWebConfiguration(Server.MapPath("~"):

The relative virtual path 'C:/...' is not allowed here.

2. OpenWebConfiguration("~")

    An error occurred loading a configuration file: Failed to map the path '/'.

我真的不知道该怎么办了。

I really don't know what to do anymore.

推荐答案

文档它指出传递一个空值的路径会打开默认的web.config文件。结果
看着你code它不会出现,你实际上是在你的路径指定配置文件名。

From the documentation it states that passing a null value for the path will open the default web.config file.
Looking at your code it doesn't appear that you are actually specifying a config filename in your path.

所以使用:

    OpenWebConfiguration(null)

编辑:O.K.我做了一些进一步的调查,并传入null将访问服务器默认的web.config文件在C:\\ WINDOWS \\ Microsoft.NET \\框架\\ V2.0.50727 \\这是不是你在这种情况下,想要什么配置文件夹

传递〜应该给你访问到Web应用程序默认的web.config文件,我已经成功地在Visual Studio 2008与.NET 3.5使用以下code测试这样的:

O.k. I have done some further investigation and passing in null will access the servers default web.config file in the C:\Windows\Microsoft.NET\Framework\v2.0.50727\Config folder which is not what you want in this case.
Passing "~" should give you access to the web applications default web.config file and I have tested this successfully in Visual Studio 2008 with .NET 3.5 using the following code:

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    config.AppSettings.Settings["test"].Value = "Hello";
    config.Save();

我不知道为什么,这是行不通的你的情况。我建议放置一个断点code线,你创建的配置对象,并使用立即窗口在Visual Studio IDE调用WebConfigurationManager的静态OpenWebConfiguration方法,看看有什么结果,你可以得到。进入类似 WebConfigurationManager.OpenWebConfiguration(〜)。AppSettings.Settings.AllKeys 应说明,如果你成功访问你的web.config。

I am not sure why this is not working your case. I would suggest placing a breakpoint at the line of code where you create your Configuration object and use the "Immediate Window" in the Visual Studio IDE to call the WebConfigurationManager's static OpenWebConfiguration method to see what result you can get. Entering something like WebConfigurationManager.OpenWebConfiguration("~").AppSettings.Settings.AllKeys should indicate if you are accessing your web.config successfully.

这篇关于如何保存配置到web.config文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆