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.
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 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 |
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 |
- Create a class that implements IHttpHandler interface
- Register the HttpHandler through code or web.config
- IsReusable
- ProcessRequest()
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
- Init()
- Dispose()
Summary:
Thats all about the Application Life cycle of Asp.Net MVC.
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 😉
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 😉
ReplyDeleteGreat post it's amazing blog Thanks a lot
Dot Net Online Training Hyderabad
Good article, I have found one more article on this topic which is easy to understand.
ReplyDeleteMVC application life cycle