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
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.
- public static void RegisterRoutes(RouteCollection 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
- );
- }
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:- protected internal virtual void ProcessRequest(HttpContextBase httpContext)
- {
- SecurityUtil.ProcessInApplicationTrust(delegate {
- IController controller;
- IControllerFactory factory;
- this.ProcessRequestInit(httpContext, out controller, out factory);
- try
- {
- controller.Execute(this.RequestContext);
- }
- finally
- {
- factory.ReleaseController(controller);
- }
- });
- }
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:- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RegisterRoutes(RouteTable.Routes);
- ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
- }
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) andActionMethodSelectorAttribute
(If more than one method found, the correct one is chosen with the help of this attribute).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.
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
andRazor
view engines. You can also register your own custom view engine to your Asp.Net MVC application as shown below:- protected void Application_Start()
- {
- //Remove All View Engine including Webform and Razor
- ViewEngines.Engines.Clear();
- //Register Your Custom View Engine
- ViewEngines.Engines.Add(new CustomViewEngine());
- //Other code is removed for clarity
- }
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的更多相关文章
- Mixing ASP.NET Webforms and ASP.NET MVC
https://www.packtpub.com/books/content/mixing-aspnet-webforms-and-aspnet-mvc *********************** ...
- Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)
上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...
- Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入
原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...
- Anti-Forgery Request Recipes For ASP.NET MVC And AJAX
Background (Normal scenario of form submitting) To secure websites from cross-site request forgery ( ...
- 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)
这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理的时候给某个角色分配权限那么就可能会出现,我所遇到的就是该角色大概200个模块每个模块平均2个功能- ...
- 解决Win10系统下 C# DateTime 出现星期几的问题 解决ASP.NET MVC 接受Request Playload参数问题
解决Win10系统下 C# DateTime 出现星期几的问题 昨天晚上写代码的时候偶然发现 DateTime 里出现了星期几,当时一阵凌乱,去网上百度没有详细解决办法,很多人说可以用用 ToStri ...
- Entity Framework在Asp.net MVC中的实现One Context Per Request(转)
上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...
- 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 “/” ...
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值
asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...
随机推荐
- 根据后端传的时间前端js进行倒计时
一.故事背景: 1. 今天公司有个项目需求 2. 在前端页面实现一个倒计时功能 3. 初步设想:后端根据需求规定一个未来的时间,前端根据当前时间进行计算 4. 然后将时间格式化,时分秒的格式 5. 时 ...
- Python 安装 pytesser 处理验证码出现的问题
今天这个问题困扰了我好久,开始直接用 pip install pytesseract 安装了 pytesseract 然后出现了如下错误 Traceback (most recent call las ...
- Elasticsearch 6.x 的分页查询数据
{ , "query": { "match" : { "person_name" : "张老师" }}, , ], &q ...
- C#socket编程序(二)
在上一篇中,我列了一些常用的方法,可以说这些方法是一些辅助性的方法,对于分析网络中的主机属性非常有用.在这篇中,我将会介绍一下面向连接(TCP)socket编程,其中辅以实例,代码可供下载. 对于TC ...
- .NETCore分布式微服务站点设计(1)-概念图
自己画了一个简略结构图,准备按照这个搭建一套微服务型的站点 利用Identityserver4+Redis+Sqlserver+Swagger+阿里云OSS+RabbitMQ+Nginx来实现,按照自 ...
- CodeIgniter2.0中sqlserver驱动返回受影响行数问题解决
最近使用CI写项目时遇到的问题,当使用sqlserve链接操作时 修改和删除返回的受影响行数不对 解决办法如下: 找到ci框架目录中include\database\drivers\sqlsrv\sq ...
- thinkphp中导入和使用阿里云OSSsdk
照做绝对行,在ThinkPHP中,第三方库都放在ThinkPHP/Library/Vendor/路径下. 1.下载OSS PHP SDK:https://help.aliyun.com/documen ...
- FPGA+ARM or FPGA+DSP?
网上有人说.现在的FPGA,ARM功能已经强大到无需DSP协助处理了,未来DSP会不会消声灭迹?是DSP取代FPGA和ARM,还是ARM,FPGA取代DSP呢?担心好不容易学精了DSP,结果DSP变成 ...
- 59. 螺旋矩阵 II
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- [leetcode shell]192. Word Frequency
统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...