当您修改 ASP.NET (MVC) 应用程序的“web.config”文件时,the application is automatically recompiled/restarted ,强制读入修改后的“web.config”。
我的问题:
是否可以将这种更改检测行为应用于 ASP.NET 网站根目录中我自己的配置文件(假设为“my-config.json”)?
即当有人修改“my-config.json”文件时,应用程序将重新启动。
最佳答案
您可以拥有FileSystemWatcher
查看您的文件并检测更改,然后重新启动应用程序或重新加载您的设置。
也许您不需要重新启动应用程序,而只需重新加载您的设置
protected void Application_Start()
{
// Other initializations ...
// ....
var watcher = new FileSystemWatcher();
//Set the folder to watch
watcher.Path = Server.MapPath("~/Config");
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
//Set a filter to watch
watcher.Filter = "*.json";
watcher.Changed += watcher_Changed;
// Begin watching.
watcher.EnableRaisingEvents = true;
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
//Restart application here
//Or Reload your settings
}
关于c# - 可以模仿我自己的配置文件的 web.config 重启行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34088822/