gpt4 book ai didi

c# - 可以为我自己的配置文件模仿 web.config 重启行为吗?

转载 作者:行者123 更新时间:2023-11-30 23:32:59 25 4
gpt4 key购买 nike

当您修改 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com