MVC Life Cycle - Part 2

Before reading this article, I recommend reading the previous par.:
In this section, we are going to delve further into the MVC Life Cycle. In the previous section, we saw the basic flow of MVC. However, let us consider the case wherein you need to inject your custom module and want to do logging extensively. In this kind of situation you want to execute some custom codes before the application actually starts. In order to achieve this kind of scenario, you need to make use of PreApplicationStartMethod @ assembly level. The following is the actual line of code for registering your custom module at PreApplicationStartMethod level.

[assembly: PreApplicationStartMethod (typeof(MvcApplication), "RegisterCustomModule")] .
But, before this let me show the snippet for the custom module which I have created here.
  1. using System.Diagnostics;  
  2. using System.Web;  
  3. namespace MVC_Life_Cycle.CustomModules   
  4. {  
  5.     public class CustomModule: IHttpModule  
  6.     {  
  7.         public void Init(HttpApplication context)  
  8.         {  
  9.             context.LogRequest += Context_LogRequest;  
  10.         }  
  11.         private void Context_LogRequest(object sender, System.EventArgs e)  
  12.         {  
  13.             Debug.WriteLine("Item Logged Successfully");  
  14.         }  
  15.         public void Dispose()   
  16.         {  
  17.             throw new System.NotImplementedException();  
  18.         }  
  19.     }  
  20. }  
This is the simple snippet wher in I am actually implementing "IHttpModule." This is a straight forward snippet. Here, at every HTTP Event it will just print the message "Item Logged Successfully." Also, I have modified the global.asax file as well. The following is the snippet for the same.
  1. using System.Diagnostics;  
  2. using System.Web;  
  3. using System.Web.Mvc;  
  4. using System.Web.Optimization;  
  5. using System.Web.Routing;  
  6. using MVC_Life_Cycle;  
  7. using MVC_Life_Cycle.CustomModules;  
  8. [assembly: PreApplicationStartMethod(typeof(MvcApplication), "RegisterCustomModule")]  
  9. namespace MVC_Life_Cycle  
  10. {  
  11.     public class MvcApplication: System.Web.HttpApplication  
  12.     {  
  13.         public static void RegisterCustomModule()   
  14.         {  
  15.             HttpApplication.RegisterModule(typeof(CustomModule));  
  16.         }  
  17.         protected void Application_Start()   
  18.         {  
  19.             AreaRegistration.RegisterAllAreas();  
  20.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  21.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  22.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  23.         }  
  24.         protected void Application_End()   
  25.         {  
  26.             Debug.WriteLine("Application Stopped");  
  27.         }  
  28.     }  
  29. }  
With the above change in place, when I run the app, then it will produce the following results:

Then, Application_Start event will get triggered.

At this stage, our custom module which we created would have gottenregistered with our other existing modules as shown below.

Now, when I inspect the output window, it will produce the following result:

Since this is HTTPModule, it will print the message number of the times HTTP-Request happens. Here, we can see that message got printed successfully. I hope you have liked this post. In the coming section, we will delve further.

Code Download