【发布时间】:2017-02-09 12:19:26
【问题描述】:
如果需要,我的Global.asax.cs 文件中有一个函数可以对Web.config 文件进行一些更改。文件保存为:
config.Save(ConfigurationSaveMode.Modified);
我最近发现TempData 不适用于我的应用程序:在下一个请求时它似乎是空的。我的MCVE:
public ActionResult Foo()
{
TempData["error"] = "testing error passing";
return RedirectToAction("Bar");
}
public ActionResult Bar()
{
throw new Exception(TempData["error"] as string);
}
我将其添加到我的HomeController,访问/Home/Foo 将重定向到/Home/Bar。但是,当上面的config.Save 行处于活动状态时,我得到:
Server Error in '/' Application.
Exception of type 'System.Exception' was thrown.
但是如果我注释掉那行,我会得到:
Server Error in '/' Application.
test error passing
正如我最初所期待的那样。
(我有几个例子得到了相反的结果,但通常是在我评论或取消评论该行之后的第一个请求,所以可能是I should blame caching。)
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
因为当 IIS 检测到配置文件的更改时,它会回收应用程序池,从而擦除所有会话数据。不过,我想不出你为什么要在运行时动态更改 web.config 的任何充分理由 - 所以我会考虑解决这个问题。
-
@AntP 好的,有道理。是的,这绝对是一个黑客。您可以将其发布为答案吗?
标签: c# asp.net asp.net-mvc tempdata