在RedirectToAction之后注入的依赖项AddTransient不更新

时间:2019-04-18 23:22:10

标签: c# asp.net-core dependency-injection

我正在实现它,因此我可以加载json文件作为选项文件,并在需要时持久保存更改后的数据。我遵循了this的问题,使文件保存工作正常,我成功获取了更改时重新加载的触发器。但是,当我POST操作并更新信息并最后执行RedirectToAction("Index")时,似乎并没有更新瞬态。返回重定向后,页面与POST之前的状态相同,但是当我手动按F5刷新页面时,现在将显示正确的数据。

我尝试过类似RedirectToAction("Index", "Admin")之类的事情,并试图让瞬态更新每个动作或编写自己的动作,但找不到任何能帮助我开始这样做的东西。

在查看上面的链接时,了解IWritableOptions是有帮助的。我唯一更改的是,它没有给它一个部分,而是加载了整个文件。

        private readonly IWritableOptions<Settings> settings;

        public AdminController(IWritableOptions<Settings> settings)
        {
            this.settings = settings;
        }

        public IActionResult Index()
        {
            // Loading for the first time all is good and settings values are valid
            // After being RedirectToAction from the HttpPost the data hasn't updated to the new values.
            // If I manually refresh the page all data is correctly set to the new values. (Same if I leave the page and come back)

            return View(settings.Value);
        }

        [HttpPost]
        public IActionResult General(SettingsViewModel model)
        {
            settings.Update(opts =>
            {
                opts.Value1 = model.Value1 ;
                opts.Value2 = model.Value1 ;
            });

            return RedirectToAction(nameof(Index));
        }

预期的结果是在RedirectToAction之后,现在将设置值设置为其新值。我认为这与使用AddTransient有关,在onReceive中,它仅在控制器完全重载时刷新,而从我的阅读中得出的结果并不是100%正。我有任何可能使它正常工作...

0 个答案:

没有答案
相关问题