简体   繁体   English

ASP.Net MVC4的哪些控制器初始化功能

[英]What controller Initialize function does ASP.Net MVC4

i am new in MVC and learning. 我是MVC和学习的新手。 here i am putting some code. 我在这里放一些代码。 so see first 所以先看

public class HomeController : BaseController
    {
        private IProductRepository productRepository;
        private string strRouteValue;

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            strRouteValue = this.ControllerContext.RouteData.Values["method"].ToString();
            this.productRepository = Factory.Create(strRouteValue);
        }

        [HttpGet]
        public ActionResult Index(int id)
        {            
            productRepository.Get(id);
            return View();
        }

        [HttpPost]
        public ActionResult Index(Product model)
        {
            productRepository.Add(model);            
            return View();
        }       
    }

what Initialize function does ? 什么是初始化函数?

every one must say this is where people would init many object, if so then we can do it in constructor of controller too. 每个人都必须说这是人们会初始化许多对象的地方,如果是这样的话,我们也可以在控制器的构造函数中进行初始化。 so what is special about controller Initialize function ? 那么控制器的初始化功能有什么特别之处呢?

what is difference between controller Initialize function and controller constructor ? 控制器初始化函数和控制器构造函数有什么区别?

Check the documentation for that method: MSDN: Controller.Initialize() : 检查该方法的文档: MSDN: Controller.Initialize()

Initializes data that might not be available when the constructor is called . 初始化在调用构造函数时可能不可用的数据。

This method cannot be called directly. 此方法不能直接调用。 Override this method in order to provide additional processing tasks before any ActionResult methods are called , such as setting the thread culture or assigning a custom provider for TempData objects. 重写此方法,以便在调用任何ActionResult方法之前提供其他处理任务,例如设置线程区域性或为TempData对象分配自定义提供程序。 If you override this method, call the base control's Initialize method. 如果重写此方法,请调用基本控件的Initialize方法。

And as I suggested on your previous twenty or so questions about MVC, Dependency Injection and controller instantiation: stop piecing together advice from poor blogposts and irrelevant answers on SO. 正如我在您先前提出的有关MVC,依赖注入和控制器实例化的大约二十个问题中所建议的那样:停止将不良博客文章中的建议和关于SO的不相关答案拼凑在一起。 Buy a decent MVC book and read it from cover to cover . 买一本不错的MVC书,然后从头到尾阅读 Then do the same with a book about Unit Testing. 然后对一本有关单元测试的书做同样的事情。 You will never get a thorough understanding of things if you continue this way. 如果继续这种方式,您将永远不会对事物有透彻的了解。

There is a difference between instantiating a controller and initializing it. 实例化控制器和初始化控制器之间是有区别的。 Instantiating is moreover a .NET concept not MVC, so every class is automatically instantiated using default constructor. 此外,实例化是.NET概念而非MVC,因此,每个类都使用默认构造函数自动实例化。 So, constructor is basically a concept of class whereas Initializing is concept of action method. 因此,构造函数基本上是类的概念,而初始化是动作方法的概念。 We override Initialize() method in order to provide additional processing tasks before any ActionResult methods are called, such as setting the thread culture or assigning TempData objects etc.... 我们重写Initialize()方法,以便在调用任何ActionResult方法之前提供其他处理任务,例如设置线程区域性或分配TempData对象等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM