通过 ASP.NET MVC 控制器更新 AppSettings
我正在编写一个基本的小论坛网络应用程序(为了好玩并磨砺旧锯子),但我在 AppSettings 方面遇到了一些麻烦。
我的计划是将这些设置放在自己的文件(Settings.config)中,我将向该文件授予 Web 进程用户帐户的修改权限,并将所有可编辑设置存储在此文件中(例如论坛标题、描述等)。
这是我的代码:
[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"); } }
...但是运行它会返回以下错误:
无法为请求的配置对象创建配置文件。
我尝试向所有用户授予对设置文件的完全权限,没有效果(我目前只是在卡西尼下运行,因此进程用户是我,无论如何都拥有该文件的所有权)。
有什么想法吗?
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.
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).
This is my 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:
A configuration file cannot be created for the requested Configuration object.
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).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将第一行更改为:
尽管可能令人困惑,OpenWebConfiguration 需要 Web.config 所在的虚拟路径,不包括文件名。我猜逻辑是任何给定目录中都只有一个 Web.config,因此不需要包含名称。
MSDN 文档最终为我们提供了线索在这里 - 如果您查看示例,它们都使用显式相对路径,并且在 IIS 下托管时允许您从其他位置指定配置文件,例如:
附录:
那么为什么
OpenWebConfiguration( "~/Web.config")
到底能用吗?我不确定我能否明确解释它,但尝试一下:将其更改为 ("~/Foo.bar")。结果一样!您可以读取 - 相同的 Web.config 文件 - 但无法写入! (现在尝试将 foo.bar 目录添加到您的站点,然后在其中放入一个 Web.config...)由于 OpenWebConfiguration 需要一个目录(并且显然允许不存在的目录,只要它在该目录中找到一个 Web.config父),我认为这就是为什么错误地将 ~/Web.config 指定为“路径”允许我们加载根配置,但不能保存它。
Change your first line to this:
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.
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:
Addendum:
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...)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.
尝试一下:
但是,我认为将此类信息存储在 Web.Config 中是一个坏主意,特别是如果它打算动态更改的话。
即使您计划使用单独的配置文件,我也宁愿以其他方式存储它,并且我不会费心通过 System.Configuration 的类来获取自己的配置。它们主要是为了读取 ASP.NET 应用程序的 Web.Config 和 Windows 应用程序的 App.Config,让它们与其他东西一起工作确实毫无意义。
我建议如下:
将单独的 XML 文件添加到您的项目中。 (最好是 App_Data 文件夹,该文件夹无法从 Web 访问,并且您的应用程序已具有读写权限。)
然后您可以将此类设置存储在该 XML 中,并使用 System 轻松读取和写入它.Xml 或 LINQ to XML。
Try this:
However, I think it is a bad idea to store this kind of information in the Web.Config, especially if it is intended to be changed dynamically.
Even if you plan to use a separate config file, I would rather store this in another way, and I wouldn't bother to get my own configuration through System.Configuration's classes. They are mostly meant to read ASP.NET apps' Web.Config and Windows apps' App.Config, and making them work with something else is really pointless.
I recommend the following:
Add a separate XML file to your project. (preferably to the App_Data folder, where it can't be accessed from the web, and where your application already has read and write permissions.)
You can then store this sort of settings in that XML, and easily read and write it using System.Xml or LINQ to XML.
如果您从另一个项目运行它,那么您将使用该文件夹中的配置,该文件夹不会有配置文件,因此会出现无法创建它的错误。
您可能想要使用 server.mappath 或类似的东西来访问 Web 文件夹配置文件。
If you are running this from another project then you'll be using the config from that folder which wont have a config file hence the error that it cannot be created.
You may want to use server.mappath or something like that to get to the web folders config file.