通过ASP.NET MVC控制器更新的AppSettings [英] Updating AppSettings via ASP.NET MVC Controller

查看:206
本文介绍了通过ASP.NET MVC控制器更新的AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个基本很少论坛Web应用程序(乐趣和锐化OLE'看到的),而我有一点麻烦的AppSettings。

I'm writing a basic little forums web app (for fun and to sharpen the ole' saw), and I'm having a bit of trouble with AppSettings.

我的计划是在自己的文件(的settings.config)这些设置,而我将给予修改权限到Web程序用户帐户,并存储在该文件中所有可编辑的设置(如论坛名称,说明等)。

My plan is to have these settings in their own file (Settings.config), to which I will grant modify permissions to the web process user account, and store all editable settings in this file (e.g. forum title, description, etc).

这是我的code:


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection collection)
{
    try
    {
        var config = WebConfigurationManager.OpenWebConfiguration("~/Web.config");

        config.AppSettings.Settings["SiteTitle"].Value = collection["SiteTitle"];
        config.AppSettings.Settings["SiteDescription"].Value = collection["SiteDescription"];

        config.Save(ConfigurationSaveMode.Minimal, false);
        ConfigurationManager.RefreshSection("appSettings");

        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("_FORM", ex.Message);
        return View("Index");
    }
}

...但运行它返回以下错误:

...but running it returns the following error:

配置文件不能被请求的配置对象创建的。

我试过授予完全权限的所有用户的设置文件,没有效果(我目前只是卡西尼下运行的,所以这个过程用户我是谁了文件的所有权在任何情况下)。

I've tried granting full permission to all users to the settings file, with no effect (I'm currently just running under Cassini, so the process user is me who has ownership of the file in any case).

任何想法?

推荐答案

您的第一行改成这样:

var config = WebConfigurationManager.OpenWebConfiguration("~");

混淆,因为它可能是,OpenWebConfiguration期望在那里的Web.config驻留的虚拟路径的不含的文件名。我猜的逻辑是,只会出现在任何给定的目录中,以便包括名也是独一无二的Web.config是不必要的。

Confusing as it may be, OpenWebConfiguration expects the virtual path where the Web.config resides, excluding the file name. I guess the logic is that there will only be one Web.config in any given directory so including the name is unnecessary.

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.openwebconfiguration.aspx\">MSDN文档最终线索我们这里 - 如果你看一下例子都使用明确的相对路径,并在IIS托管时,允许你从其他位置指定配置文件,例如:

The MSDN documentation ultimately clues us in here - if you look at the examples they all use explicit relative paths, and when hosting under IIS allow you to specify config files from other locations, for example:

OpenWebConfiguration("/siteName", "Default Web Site", null, "myServer");

附录:

那么,为什么 OpenWebConfiguration(〜/ Web.config文件)的工作呢?我不知道我可以明确地解释,但试试这个踢:将其更改为(〜/ Foo.bar​​)。同样的结果!你可以阅读 - 的您同样Web.config文件的 - 但不能写! (现在尝试添加foo.bar目录到你的网站,然后把一个Web.config里面......)

So why does OpenWebConfiguration("~/Web.config") work at all? I'm not sure I can definitively explain it, but try this for kicks: change it to ("~/Foo.bar"). The same result! You can read - your same Web.config file - but cannot write! (now try adding the foo.bar directory to your site, then put a Web.config inside it...)

由于OpenWebConfiguration期望一个目录(显然允许不存在的人,只要它发现在父一个Web.config),我想这就是为什么误指定〜/ Web.config中作为路径让我们加载根的配置,但不保存它。

Since OpenWebConfiguration expects a directory (and apparently allows non-existent ones, as long as it finds a Web.config within the parent), I think this is why mistakenly specifying ~/Web.config as a "path" allows us to load the root config, but not save it.

这篇关于通过ASP.NET MVC控制器更新的AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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