C# ASP.NET MVC 控制器生命周期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1763775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:36:36  来源:igfitidea点击:

ASP.NET MVC Controller Lifecycle

c#asp.net-mvc

提问by Josh Pearce

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?

我的理解是在每个 Web 请求期间都不会调用控制器的构造函数。假设这是真的,控制器的生命周期是什么?是在应用程序启动时“构建”,然后缓存并调用每个 Web 请求中注入的 requestcontext 吗?

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.

明确地说,我不是在问如何模拟构造函数的行为,我使用 OnActionExecuting 事件来启动我通常在构造函数中执行的操作。此外,我确实在控制器上使用构造函数进行单元和系统测试。

Thanks!

谢谢!

采纳答案by Darin Dimitrov

If you use the default controller factorya 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.

如果您使用默认控制器工厂,将为每个请求构造一个新实例,这就是它应该的方式。控制器不应在不同的请求之间共享。您可以编写一个自定义工厂来管理控制器的生命周期。

回答by Thomas Weller

I'm afraid, your understanding is wrong. A controller (which should be a very thin and lightweight class and must not have any session-outliving state) is actually constructed on the fly for each and every web request. How else could a controller instance be specific to a certain view?

恐怕,你的理解是错误的。控制器(它应该是一个非常瘦和轻量级的类,并且不能有任何会话过期状态)实际上是为每个 Web 请求动态构建的。控制器实例如何特定于某个视图?

So there is no such thing as a "lifecycle" (other than that of the request)...

所以没有“生命周期”这样的东西(除了请求的生命周期)......

回答by Hareendra Chamara Philips

A controller is created for every request you do. Lets take an example.

为您执行的每个请求创建一个控制器。让我们举个例子。

   public class ExampleController : Controller{
           public static userName;

            public void Action1(){//do stuff}
            public void Action2(){//do stuff}
            public void AssignUserName(string username){
                 userName = username;

            }
           public string GetName(){ return userName;}


   }

Now you can call the controller from the view passing a username. Don't hope to get the userName you set in the next request. it will return null. Thus for every request a new controller is created. You don't instantiate a controller anywhere in MVC like you instatiate an object from a class. Simply you don't have controller object memory pointer to call it as you do with other objects.

现在您可以从传递用户名的视图中调用控制器。不要希望得到你在下一个请求中设置的用户名。它将返回空值。因此,对于每个请求,都会创建一个新的控制器。您不会像从类中实例化对象那样在 MVC 中的任何地方实例化控制器。只是你没有控制器对象内存指针来调用它,就像你对其他对象一样。

Go to this link. There is a good explanation on lifecycle of MVC controller.

转到此链接。MVC 控制器的生命周期有一个很好的解释。

ASP.Net MVC - Request Life Cycle

ASP.Net MVC - 请求生命周期