Updating AppSettings via ASP.NET MVC Controller

前端 未结 3 596
既然无缘
既然无缘 2021-02-14 00:25

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 i

3条回答
  •  执念已碎
    2021-02-14 00:58

    Try this:

    var configFile = HttpContext.Current.Server.MapPath("~/Web.config");
    var config = WebConfigurationManager.OpenWebConfiguration(configFile);
    

    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.

提交回复
热议问题