【发布时间】:2019-06-01 22:03:50
【问题描述】:
我检测到,在加载主页期间,会实例化几个控制器(我认为是因为主页是由几个部分构建的)。控制器实例化 API 类以通过它们查询一些数据。我想知道如何以及在哪里可以在它们之间共享相同的 API 类实例。
我可以想象这样的代码:
class HomeController : Controller
{
private MyApi Api;
public HomeController()
{
this.Api = get the pervious MyApi instance form somewhere
if (this.Api == null) // 1st time
{
this.Api = new MyApi();
put this instance to somewhere to share between controllers
}
这个“某处”不是会话,因为下一页加载需要另一个 MyApi 实例。它必须转到在整个页面加载过程中保持不变的对象属性,但在生成 html 结果时将其关闭。这一定很简单,但我真的不知道它在哪里:(有人可以帮我吗?
【问题讨论】:
标签: c# model-view-controller controller