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.

Asp.net MVC Request Life Cycle的更多相关文章

  1. Mixing ASP.NET Webforms and ASP.NET MVC

    https://www.packtpub.com/books/content/mixing-aspnet-webforms-and-aspnet-mvc *********************** ...

  2. Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  3. Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入

    原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...

  4. Anti-Forgery Request Recipes For ASP.NET MVC And AJAX

    Background (Normal scenario of form submitting) To secure websites from cross-site request forgery ( ...

  5. 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)

    这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理的时候给某个角色分配权限那么就可能会出现,我所遇到的就是该角色大概200个模块每个模块平均2个功能- ...

  6. 解决Win10系统下 C# DateTime 出现星期几的问题 解决ASP.NET MVC 接受Request Playload参数问题

    解决Win10系统下 C# DateTime 出现星期几的问题 昨天晚上写代码的时候偶然发现 DateTime 里出现了星期几,当时一阵凌乱,去网上百度没有详细解决办法,很多人说可以用用 ToStri ...

  7. Entity Framework在Asp.net MVC中的实现One Context Per Request(转)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  8. ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll

    ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll “/” ...

  9. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

随机推荐

  1. 前端程序员必知的30个Chrome扩展-[转载]

    谷歌Chrome浏览器是网络上可用的最好浏览器之一,并且自2011年11月超越了Firefox浏览器之后,已经成为了互联网上占主导地位的浏览器.今天,HTML5中国与大家分享一些实用的谷歌Chrome ...

  2. 2017-2018-2 20165301 实验四《Java面向对象程序设计》实验报告

    2017-2018-2 20165301 实验四<Java面向对象程序设计>实验报告 一.Android Stuidio的安装测试 实验要求: 参考<Java和Android开发学习 ...

  3. PHP XML操作的各种方法解析

    PHP提供了一整套的读取 XML文件的方法,很容易的就可以编写基于 XML的脚本程序.本章将要介绍 PHP与 XML的操作方法,并对几个常用的 XML类库做一些简要介绍. XML是一种流行的半结构化文 ...

  4. 自己封装的php Curl并发处理,欢迎提出问题优化。

    因为项目需要,发现一个一个发送请求实在太慢,无奈之下,我们可以封装一个并发处理的curl请求批处理句柄来减少重复创建句柄的问题 代码如下: /* *@param array $data url的参数 ...

  5. 【POJ】1740.A New Stone Game

    题解 想去学习一下博弈论的SG函数 不过貌似这道题就是猜结论并且证明 题意是,随便选择一堆石子,扔掉至少一个,然后从扔石子的这堆里选择任意多(可以不选)放到其他任意多的未选择完的石堆里 一堆石子,先手 ...

  6. Netty 实现SSL安全连接(wss://)

    原文:Netty5使用自签证书实现SSL安全连接 在客户端是https:// 协议下,使用ws:// 协议连接会报错的,得需要使用wss:// 连接.(ip连接失败时使用域名连接) netty创建服务 ...

  7. MYSQL插入不能中文的问题的解决

    这个问题是由于数据库的字符集不对的问题. 解决方法: 打开要用的数据库,输入命令 status 如果Client characterset 值为utf8,则要改为:set char set 'gbk' ...

  8. Windows-caffe配置

    我在博客园的第一篇博客开始了,好紧张,好激动,好淫荡...哈哈O(∩_∩)O哈! 下面开始进入正题.配置环境为windows7+cuda7.5+vs2013+matlab2014a 之前用的happy ...

  9. Eclipse 更改默认的编码 和 换行符

  10. Eclipse导入SVN项目的三种方式

    Eclipse导入SVN项目的三种方式 一.直接Import导入: 1.点击 File --> Import,进入导入项目窗口 2.选择从SVN检出项目,点击Next 3.选择创建新的资源库位置 ...