Q.Explain “Page Life Cycle” of an ASP.NET MVC?
Ans.In traditional ASP.NET webform application each asp.net page implements the IHTTPHandler interface.
This interface has a ProcessRequest() method that gets called when you request the page. The ProcessRequest() method is responsible for processing the request and generating the response. So in asp.net application it is simple, you request for a page in the url like http://mylocalsite1\default.aspx and then it search for that page on the local disk and execute the processrequest method and generate the html response.
However in MVC application ,request response process is different from webform. In MVC there are no physical page exist for a particular request. All the requests are routed to a special class called Controller. The controller is responsible for generating the response and sending the content back to the browser.
When you build a MVC application you can define a bunch of controllers. Each controller can handle multiple requests. For example, all the following will route to same Controller.
These requests will execute the same Controller Controller1 and in that it will extract the id from the URL and process the request. This gives more readable URL then asp.net page.
So how these requests are routed to particular controller. Now the RouteTable come into the picture. Every ASP.NET MVC application has a RouteTable. This routetable is responsible to map the mvc requests to particular controller.
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollction routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
{
public static void RegisterRoutes(RouteCollction routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
When Application Start the Application_Start events will get called and it will call the method RegisterRoutes which will send the collection of Routes as the parameter and the routes will be added to this Collection which is a Dictionary of NamedMaps.
Each route also references a handler which will handle the rest of the request. When we use MapRoute, what we really do is creating a route by instantiating the 3 dictionaries and defining the default MVC handler: PageRouteHandler. Each RouteHandler implements an interface IRouteHandler.
public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionarydefaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
{ if (routeUrl == null) { throw new ArgumentNullException("routeUrl"); } Route item = new Route(routeUrl, defaults, constraints, dataTokens, new PageRouteHandler(physicalFile, checkPhysicalUrlAccess)); this.Add(routeName, item); return item; }
Step 2: The URL Routing Module intercepts the request.
Whenever you make a request against an ASP.NET MVC application, the request is intercepted by the UrlRoutingModule HTTP Module.
When the UrlRoutingModule intercepts a request, the first thing the module does is to wrap up the current HttpContext in an HttpContextWrapper object.
The HttpContextWrapper object derives from HTTPContextBase class.
private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{ HttpApplication application = (HttpApplication) sender; HttpContextBase context = new HttpContextWrapper(application.Context); this.PostResolveRequestCache(context); }
Then the module send this contextBase object to the method called PostResolveRequestCache .
Based on the Context it will return the correct RouteData from the RouteTable which was created in the earlier step based on the URL, form parameters and query string parameters associated with the HTTPContext object.
If the UrlRoutingModule successfully retrieves a RouteData object then the module next creates a RouteContext object that represents the current HttpContext and RouteData.
The module then instantiates a new HttpHandler based on the RouteTable and passes the RouteContext to the new handler’s constructor.
In the case of an ASP.NET MVC application,the handler returned from the RouteTable will always be an MvcHandler. This MVCHandler also derive from interface IHTTPHandler and implement the method ProcessRequest().
The last step is it will call the RemapHandler method which will set the MVCHandler just obtained to be the Current HTTP Handler.
public virtual void PostResolveRequestCache(HttpContextBase context)
{ RouteData routeData = this.RouteCollection.GetRouteData(context); if (routeData != null) { IRouteHandler routeHandler = routeData.RouteHandler; if (routeHandler == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[0])); } if (!(routeHandler is StopRoutingHandler)) { RequestContext requestContext = new RequestContext(context, routeData); context.Request.RequestContext = requestContext; IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext); if (httpHandler == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("UrlRoutingModule_NoHttpHandler"), new object[] { routeHandler.GetType() })); } if (httpHandler is UrlAuthFailureHandler) { if (!FormsAuthenticationModule.FormsAuthRequired) { throw new HttpException(0x191, SR.GetString("Assess_Denied_Description3")); } UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this); } else { context.RemapHandler(httpHandler); } } } }
Step 4: MVC Handler Executes
MVCHandler also inherit from the IHTTPAsyncHandler. When MVC Handler executes it will call the BeginProcessRequest method of the httpAsyncHandler asynchronously.
When the process request method is called a new controller gets created. The controller is created from a ControllerFactory. There is a ControllerBuilder Class which will set the ControllerFactory.
You can create your own ControllerFactory as well but by default it will be DefaultControllerFactory. The RequestContext and the name of the Contoller will be passed to the method CreateController Method to get the particular Contoller.
|
elazığ
ReplyDeletebilecik
kilis
sakarya
yozgat
GCD
21CFC
ReplyDeleteBartın Parça Eşya Taşıma
Osmaniye Şehir İçi Nakliyat
Balıkesir Şehir İçi Nakliyat
Silivri Cam Balkon
Sivas Evden Eve Nakliyat
Altındağ Fayans Ustası
Çerkezköy Evden Eve Nakliyat
Pursaklar Fayans Ustası
Ünye Mutfak Dolabı
50B32
ReplyDeleteBursa Canlı Görüntülü Sohbet
Erzincan Sohbet Uygulamaları
osmaniye görüntülü sohbet uygulamaları ücretsiz
burdur mobil sohbet sitesi
Kocaeli Sohbet
Eskişehir Canlı Sohbet Ücretsiz
adıyaman parasız görüntülü sohbet uygulamaları
Kırşehir Mobil Sesli Sohbet
şırnak en iyi ücretsiz görüntülü sohbet siteleri
2515A
ReplyDeletecanlı sohbet et
aydın canlı sohbet et
karabük mobil sohbet
samsun kadınlarla ücretsiz sohbet
kayseri sohbet
agri bedava görüntülü sohbet
canlı sohbet odası
Antalya En İyi Sesli Sohbet Uygulamaları
van sesli sohbet odası
DFA92
ReplyDeleteTiktok Beğeni Hilesi
Mexc Borsası Güvenilir mi
Binance Ne Kadar Komisyon Alıyor
Bitcoin Giriş Nasıl Yapılır
Bitcoin Oynama
Nonolive Takipçi Hilesi
Bitcoin Madenciliği Siteleri
Periscope Beğeni Hilesi
Facebook Sayfa Beğeni Satın Al
9F02E
ReplyDeleteOrt Coin Hangi Borsada
Threads Beğeni Satın Al
Wabi Coin Hangi Borsada
Bitcoin Nasıl Para Kazanılır
Nonolive Takipçi Hilesi
Btcst Coin Hangi Borsada
Coin Nasıl Alınır
Likee App Beğeni Hilesi
Binance Hesap Açma
E9393
ReplyDeleteAlya Coin Hangi Borsada
Referans Kimliği Nedir
Referans Kimliği Nedir
Milyon Coin Hangi Borsada
Azero Coin Hangi Borsada
Coin Madenciliği Nedir
Binance Referans Kodu
Binance Referans Kodu
Mexc Borsası Kimin
F20C3A9740
ReplyDeletetürk takipçi satın al
BF3AB903D4
ReplyDeletebayan takipci
FBF8413CF5
ReplyDeleteinstagram türk kadın takipçi
9F2F7B51BE
ReplyDeleteinstagramda takipci artirmak
A5BC3D2A06
ReplyDeletetürk instagram takipçi