I'm developing an ASP.NET application using the MVC 5 framework. This application will ultimately be deployed on-premise. Therefore, users need to be able to install and configure the application before they can start using it. I need them to be able to specify things like the database name (MSSQL), the locations of various supporting services, and certain credentials like API keys for third-party services.
In a few MVC 5 applications I've used, these settings could be managed through a form. So I thought of creating a Configurations
controller with GET and POST Edit actions. So the user can install the app, and then go to http://myServer/myApp/Config and specify the various settings there.
What I'm not sure about is where those settings would then be stored. Would it be the web.config file, or a settings.xml file, or the database?
Here's the strange thing... I already have a working solution with web.config
that uses the below code:
public class ConfigController : Controller
{
// GET: Config/Edit/5
[HttpGet]
public ActionResult Edit()
{
ViewBag.DatabaseServer = WebConfigurationManager.AppSettings["DatabaseServer"];
ViewBag.DatabaseName = WebConfigurationManager.AppSettings["DatabaseName"];
ViewBag.PusherClientID = WebConfigurationManager.AppSettings["ClientID"];
ViewBag.PusherAPIKey = WebConfigurationManager.AppSettings["APIKey"];
return View();
}
// POST: Config/Edit/5
[HttpPost]
public ActionResult Edit(FormCollection collection)
{
WebConfigurationManager.AppSettings.Set("DatabaseServer", collection.Get("databaseserver"));
WebConfigurationManager.AppSettings.Set("DatabaseName", collection.Get("databasename"));
WebConfigurationManager.AppSettings.Set("ClientID", collection.Get("pusherclientid"));
WebConfigurationManager.AppSettings.Set("APIKey", collection.Get("pusherapikey"));
return RedirectToAction("Edit");
}
}
And my web.config
file:
<appSettings>
...
<add key="DatabaseServer" value="localhost\sqlexpress" />
<add key="DatabaseName" value="MyDatabase" />
<add key="ClientID" value="testID" />
<add key="APIKey" value="testkey" />
</appSettings>
I can change these variables on a form on edit.cshtml
and they persist fine. Two problems:
- I can't figure out where they go. I'm told they go in
web.config
but I checked bothweb.config
files inside my app and none of them have the updated values. On the form, I update ClientID to "123" but inweb.config
it still says "testID" which is the original default value. - I'm told that updating
web.config
will cause the app pool to restart. Yet that doesn't seem to be the case here...
Can someone explain what is going on?