• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 6, 2017

Application Life cycle of Asp.Net MVC

 Abhishek Tomer     August 06, 2017     Asp.Net, Asp.Net MVC     2 comments   

Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.Net MVC or want to refresh their knowledge about the application life cycle.

In today's world many of the developers doesn't know about how the application executes or how the requests and responses are handled by the application.

In this article your all concepts will get cleared and if you think that i have left something to mention in this post then please comment below so that i can update this post and help you.

So let's start now.

Any web application has two main application execution steps that events that are associated with start and end events of the application. Application start initiates when the application is executed for the very first time. Application end event is fired when application has been shut down.

It’s important to understand Web application life cycle events to get a better understanding on how ASP.NET MVC life cycle starts. In our last article of page lifecycle of Asp.Net MVC we have learned about the URL routing is the starting point for MVC application that has a collection of predefined routes to map from.

Note : URL routing handler gets this information from Application start event. These events are registered in Global.asax file which contains all the application level events. All the Application level Events are handled by Global.asax file.

Application_Start event is:
  • An event that fires when first request is received.
  • Can be used to run initial configuration and settings code.
  • The event takes care of registering all areas of MVC application, installing global filters, adding routes and bundles.
As I have mentioned Application_Start is the first event that gets called when the application is requested for the very first time, all the pre-application tasks like routing takes place here.

As you can see in the diagram below ResolveRequestCache needs to know the routes to choose from, and this needs to have static route collection that are already been created.
Request lifecycle of Asp.Net MVC
Request lifecycle of Asp.Net MVC
PreApplicationStart:
It is another option at the assembly level to register something before the application starts. It could be used to run some initial configuration code or register modules or any other code that needs to be executed before the application starts.

HttpHandlers:
These are the classes that implement IHttpHandler and generate a response to HttpRequest. There could be httpHandler re-assignment in a life cycle of a request but only one httpHandler executes and provides response at a time.

There are two sets of events in the Asp.Net MVC  page request life cycle that concerns HttpHandlers, (MapRequestHandler and PostMapRequestHandler) and (RequestHandlerExecute and PostRequestHandlerExecute).

MapRequestHandler and PostMapRequestHandler are the events in the life cycle which determines the httpHandler responsible for executing the request. Only the selection happens during this time.

RequestHandlerExecute and PostRequestHandlerExecute are the life cycle events that actually executes the htttp handler determined in the earlier phases of request life cycle.
HttpHandler in Asp.Net MVC Request life cycle
HttpHandler in Asp.Net MVC Request life cycle
Creating an HttpHandler:
  • Create a class that implements IHttpHandler interface
  • Register the HttpHandler through code or web.config
IHttpHandler exposes two members:
  1. IsReusable
  2. ProcessRequest()
DemoHandler.cs Code:
public class DemoHandler : IHttpHandler
    {
        public bool IsReusable { get { return false; } }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("This is the DemoHandler");
        }
    }

HttpModules:
These are classes the that implements IHttpModule interface and are designed to respond to Life cycle events. In a given Http life cycle, multiple Http modules can respond to one single request and can also hook into multiple life cycle events. So they are not tied to any specific event, rather they can act at several places during the life cycle and expose multiple development possibilities.

One of the advantage of HttpModules is that it bring in the reusability, modules written once can be reused any several application across frameworks. Features such as logging and authentication are best examples of wrapping things up in a HttpModule.

Note : One can also do the all these things possible in HttpModule in a Global.asax file, but that won’t achieve reusability and abstraction.

Creating a HttpModule
  • Implement the IHttpModule interface
  • Register the HttpModule through code or web.config
IHttpModule exposes two members:
  1. Init()
  2. Dispose()
Summary:
Thats all about the Application Life cycle of Asp.Net MVC.
in our next session we will be learning differences between HttpModule and HttpContext.

I hope you have enjoyed this post.
Please share this post with your friends and colleagues to help them clearing the interview.

For any queries please post a comment below.

Happy Coding ðŸ˜‰
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

2 comments:

  1. rmouniakJuly 27, 2018 at 5:23 PM


    Great post it's amazing blog Thanks a lot
    Dot Net Online Training Hyderabad

    ReplyDelete
    Replies
      Reply
  2. VikramMay 22, 2020 at 4:39 PM

    Good article, I have found one more article on this topic which is easy to understand.
    MVC application life cycle

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...
  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape