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

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

问题描述

我正在写一个基本的小论坛网络应用程序(为了乐趣和锐化ole'saw),我有一点麻烦与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).

这是我的代码:


[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:

无法为所请求的配置对象创建配置文件。

试图向所有用户授予对设置文件的完全权限,但没有效果(我目前只在Cassini下运行,因此进程用户是在任何情况下都具有该文件所有权的用户)。

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.

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天全站免登陆