0

i am new in MVC and learning. 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 ?

2 Answers 2

8

For reference, here is the current MSDN documentation for Controller.Initialize(RequestContext):

Initializes data that might not be available when the constructor is called.

This method cannot be called directly, but it can be overridden 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. Note that if you override this method, your override should also call the base controller's Initialize(RequestContext) method.

Sign up to request clarification or add additional context in comments.

3 Comments

can i set thread culture, assign temp data from controller constructor ? if possible then why some one will override initialize() function? i just like to know what is the difference between controller initialize() function and controller constructor ? if u know the answer then share with me.
I know the answer, and posted it in bold.
@CodeCaster would you please let me know a good book that i can read for gaining good knowledge of MVC, Dependency Injection and other good stuffs.
2

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. 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....

2 Comments

i post a question to the comment section of @codecaster. please see and answer if possible.
Initializes data that might not be available when the constructor is called. For example, if you want to set a session then you cannot do it in constructor because HttpContext.Session will always be null there. Whereas you can do it in Intialize method. Another example, you would like to know if the request is Authenticated using HttpContext.Request.IsAuthenticated but you cannot do it in constructor as HttpContext will always be null there. I hope its pretty clear now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.