ASP.NET MVC的生命周期控制器 [英] ASP.NET MVC Controller Lifecycle
问题描述
这是我的理解,对于一个控制器构造每个Web请求期间不叫。假设这是真的,什么是控制器的生命周期?难道又是建造在应用程序启动,然后缓存并注入它与每个Web请求的RequestContext调用?
It's my understanding that the constructor for a controller is not called during each web request. Assuming this is true, what is the lifecycle of a controller? Is is "constructed" upon app start, then cached and invoked with the requestcontext injected into it with each web request?
只是要清楚,我不要求如何模拟构造的行为,我用的是OnActionExecuting事件引发的事情,我会在构造通常做。另外,我使用的构造上的控制器进行单元测试和系统测试。
Just to be clear, I'm not asking how to emulate constructor behavior, I use the OnActionExecuting event to initiate things I would normally do in a constructor. Also, I do use constructors on controllers for unit and system testing.
谢谢!
推荐答案
如果您使用的默认的控制器工厂的一个新的实例将建设为每个请求,这就是事情应该是这样。控制器应该没有什么不同请求之间共享。你可以尽管编写管理控制器的寿命定制工厂。
If you use the default controller factory a new instance will be constructed for each request and that's the way it should be. Controllers shouldn't be shared among different requests. You could though write a custom factory that manages the lifetime of the controllers.
这篇关于ASP.NET MVC的生命周期控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!