zoukankan      html  css  js  c++  java
  • Asp.net MVC Request Life Cycle

    Asp.net MVC Request Life Cycle

    While programming with Asp.net MVC, you should be aware of the life of an Asp.net MVC request from birth to death. In this article, I am going to expose the Asp.net MVC Request Life cycle. There are seven main steps that happen when you make a request to an Asp.net MVC web applications. For more details refer Detailed ASP.NET MVC Pipeline

    1. Routing

      Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table.

      When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests that match these patterns. An application has only one Route Table and this is setup in the Global.asax file of the application.

      1. public static void RegisterRoutes(RouteCollection routes)
      2. {
      3. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      4. routes.MapRoute( "Default", // Route name
      5. "{controller}/{action}/{id}", // URL with parameters
      6. new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
      7. );
      8. }
    2. MvcHandler

      The MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler implements IHttpHandler interface and further process the request by using ProcessRequest method as shown below:

      1. protected internal virtual void ProcessRequest(HttpContextBase httpContext)
      2. {
      3. SecurityUtil.ProcessInApplicationTrust(delegate {
      4. IController controller;
      5. IControllerFactory factory;
      6. this.ProcessRequestInit(httpContext, out controller, out factory);
      7. try
      8. {
      9. controller.Execute(this.RequestContext);
      10. }
      11. finally
      12. {
      13. factory.ReleaseController(controller);
      14. }
      15. });
      16. }
    3. Controller

      As shown in above code, MvcHandler uses the IControllerFactory instance and tries to get a IController instance. If successful, the Execute method is called. The IControllerFactory could be the default controller factory or a custom factory initialized at the Application_Start event, as shown below:

      1. protected void Application_Start()
      2. {
      3. AreaRegistration.RegisterAllAreas();
      4. RegisterRoutes(RouteTable.Routes);
      5. ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
      6. }
    4. Action Execution

      Once the controller has been instantiated, Controller's ActionInvoker determines which specific action to invoke on the controller. Action to be execute is chosen based on attributes ActionNameSelectorAttribute (by default method which have the same name as the action is chosen) and ActionMethodSelectorAttribute(If more than one method found, the correct one is chosen with the help of this attribute).

    5. View Result

      The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The result type can be ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.

    6. View Engine

      The first step in the execution of the View Result involves the selection of the appropriate View Engine to render the View Result. It is handled by IViewEngine interface of the view engine. By default Asp.Net MVC usesWebForm and Razor view engines. You can also register your own custom view engine to your Asp.Net MVC application as shown below:

      1. protected void Application_Start()
      2. {
      3. //Remove All View Engine including Webform and Razor
      4. ViewEngines.Engines.Clear();
      5. //Register Your Custom View Engine
      6. ViewEngines.Engines.Add(new CustomViewEngine());
      7. //Other code is removed for clarity
      8. }
    7. View

      Action method may returns a text string,a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and returns an HTML page to the browser by using the current view engine.

    What do you think?

    I hope you will enjoy the Asp.Net MVC request life cycle while programming with Asp.Net MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

  • 相关阅读:
    Oracle数据中的Regexp_*的大概用法(正则表达式)REGEXP_LIKE、REGEXP_INSTR 、REGEXP_SUBSTR 、REGEXP_REPLACE
    Oracle 基本权限授权或者回收权限、解锁等权限配置
    linux导入导出数据库方法 windows导入导出数据库方法
    新安装 wampserver 出现 You don't have permission to access / on this server. 或者访问数据库出现You don't have permission to access /phpmyadmin/ on this server.(解决方法)转
    非常的奇葩,终于解决了硬盘从盘盘符消失的问题 http://bbs.gamersky.com/thread-1712710-1-1.html (出处: 游民星空论坛)
    Oracle 检验身份证是否正确的存储过程
    PHP语言、浏览器、操作系统、IP、地理位置、ISP
    解决weblogic与系统时间相差8小时的问题
    nagios中监测dns 227.7.128.68的网络状态
    【转】新手该如何学python怎么学好python?
  • 原文地址:https://www.cnblogs.com/zjoch/p/4666815.html
Copyright © 2011-2022 走看看