Wednesday, June 13, 2012

Route of executing in Controller of ASP.NET MVC

Base classes of controller is ASP.NET MVC have number of methods for initialization before your code runs in an action. Lets review them.

  • First of all about constructor. You can't do a lot in it because most of properties aren't initialized yet and you will get NullReferenceException if try to use them.
  • Execute is inherited from IController interface and manages all other initialization methods. Usually you have to avoid override it.
  • Initialize sets ControllerContext. You should to override it if you need to set culture so on.
  • ExecuteCore is called to process request and responsible for loading TempData.
  • If I want to predefine some information (grab data from Session and so on) You should use OnActionExecuting. This methods is called just before executing your action method.
  • And finally you should use OnActionExecuted for any finalization after executing your code. 

PS: During preparing for this post I've used ILSpy. It is excellent free substitute for .NET Reflector (it is payment tool now).

1 comment:

micle said...

Great Article

We can also submit our .net related links on http://www.dotnettechy.com to improve traffic.

The dotnettechy.com is a community of .Net developers joined together to learn, to teach, to find solutions, to find interview questions and answers, to find .net website / blog collection and to have fun programming.