[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(三)
使用特殊方法处理请求生命周期事件
为了在全局应用类中处理这些事件,我们会创建一个名称以 Application_ 开头,以事件名称结尾的方法,比如 Application_BeginRequest。举个例子,就像 Application_Start 和 Application_End 方法,ASP.NET 框架就会在事件触发的时候找到这些函数并触发它。下面是更新后的代码片段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace SimpleApp
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
} protected void Application_BeginRequest()
{
RecordEvent("BeginRequest");
} protected void Application_AuthenticateRequest()
{
RecordEvent("AuthenticateRequest");
} protected void Application_PostAuthenticateRequest()
{
RecordEvent("PostAuthenticateRequest");
} private void RecordEvent(string name)
{
List<string> eventList = Application["events"] as List<string>;
if (eventList == null)
{
Application["events"] = eventList = new List<string>();
}
eventList.Add(name);
}
}
}
我定义了一个叫做 RecordEvent 的方法,用来接收一个事件的名称作为参数,并将其存储到 HttpApplication 类的 Application 属性中。
注意:在没有深入了解 Application 属性之前,请勿滥用这个属性。
我从添加到全局应用类中的其他三个方法中调用了 RecordEvent 方法。这些方法会在 BeginRequest, AuthenticateRequest 和 PostAuthenticateRequest 触发的时候被调用。我们暂时不需要将这些函数显式注册成事件处理器,ASP.NET 框架会自动定位和调用这些函数。
展示事件信息
为了展示我们代码中接收到的事件的信息,我们需要更改 Home controller 和它的 Index 视图。代码如下:
using SimpleApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace SimpleApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(HttpContext.Application["events"]);
} [HttpPost]
public ActionResult Index(Color color)
{
Color? oldColor = Session["color"] as Color?; if (oldColor != null)
{
Votes.ChangeVote(color, (Color)oldColor);
}
else
{
Votes.RecordVote(color);
} ViewBag.SelectedColor = Session["color"] = color;
return View(HttpContext.Application["events"]);
}
}
}
为了获取到存储在全局应用类中的数据,我们需要使用到 HttpContext.Application 属性,我们后面会详细讲解上下文对象。现在,我们需要更新相关的 Razor 视图:
@using SimpleApp.Models
@model List<string>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Vote</title>
<link rel="stylesheet" href="~/Content/bootstrap.min.css" />
<link rel="stylesheet" href="~/Content/bootstrap-theme.min.css" />
</head>
<body class="container">
<div class="panel panel-primary"> @if (ViewBag.SelectedColor == null)
{
<h4 class="panel-heading">Vote for your favourite color</h4>
}
else
{
<h4 class="panel-heading">Change your vote from @ViewBag.SelectedColor</h4>
} <div class="panel-body">
@using (Html.BeginForm())
{
@Html.DropDownList("color", new SelectList(Enum.GetValues(typeof(Color))), "Choose a Color", new { @class = "form-control" }) <div>
<button class="btn btn-primary center-block" type="submit">Vote</button>
</div>
}
</div>
</div> <div class="panel panel-primary">
<h5 class="panel-heading">Results</h5> <table class="table table-striped table-condensed">
<tr>
<th>Color</th>
<th>Votes</th>
</tr>
@foreach (Color c in Enum.GetValues(typeof(Color)))
{
<tr>
<td>@c</td>
<td>@Votes.GetVotes(c)</td>
</tr>
}
</table>
</div> <div class="panel panel-primary">
<h5 class="panel-heading">Events</h5>
<table class="table table-condensed table-striped">
@foreach (string eventName in Model)
{
<tr><td>@eventName</td></tr>
}
</table>
</div>
</body>
</html>
事件名称列表作为模型对象传递到视图中,我们使用 Razor foreach 循环来生成 HTML table 元素。

图 1 - 展示生命周期事件详情
提示:这种技术只能使用在排在 PreRequestHandlerExecute 事件之前的事件之上,因为 controller 中的 action 方法会在 PreRequestHandlerExecute 和 PostRequestHandlerExecute 事件之间执行,所以后续触发的事件都已经在响应生成好之后发生了。
[根据 Adam Freeman – Pro ASP.NET MVC 5 Platform 选译]
[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(三)的更多相关文章
- C# MVC 5 - 生命周期(应用程序生命周期&请求生命周期)
本文是根据网上的文章总结的. 1.介绍 本文讨论ASP.Net MVC框架MVC的请求生命周期. MVC有两个生命周期,一为应用程序生命周期,二为请求生命周期. 2.应用程序生命周期 应用程序生命周期 ...
- $Django orm增删改字段、建表 ,单表增删改查,Django请求生命周期
1 orm介绍 ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,“Object Relational Mapping”,即对象-关系映射,简称ORM. 一 ...
- django请求生命周期流程与路由层相关知识
目录 请求生命周期流程图 路由层之路由匹配 无名有名分组 反向解析 无名有名分组反向解析 路由分发 名称空间 请求生命周期流程图 django请求生命周期流程图 路由层之路由匹配 我们都知道,路由层是 ...
- [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)
不使用特殊方法来处理请求生命周期事件 HttpApplication 类是全局应用类的基类,定义了可以直接使用的一般 C# 事件.那么使用标准 C# 事件还是特殊方法那就是个人偏好的问题了,如果喜欢, ...
- [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(二)
ASP.NET 请求生命周期 全局应用类也可以用来跟踪每个独立请求的生命周期,包括请求从 ASP.NET 平台传递到 MVC 框架.ASP.NET 框架会创建一个定义在 Global.asax 文件中 ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(六)
使用 HttpApplication 对象 ASP.NET 框架中的许多类都提供了许多很方便的属性可以直接映射到 HttpContext 类中定义的属性.这种交叠有一个很好的例子就是 HttpAppl ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(五)
ASP.NET 上下文对象 ASP.NET 提供了一系列对象用来给当前请求,将要返回到客户端的响应,以及 Web 应用本身提供上下文信息.间接的,这些上下文对象也可以用来回去核心 ASP.NET 框架 ...
- [译] ASP.NET 生命周期 – ASP.NET 应用生命周期(一)
概述 ASP.NET 平台定义了两个非常重要的生命周期.第一个是 应用生命周期 (application life cycle),用来追踪应用从启动的那一刻到终止的那一刻.另一个就是 请求生命周期 ...
- 详解ASP.NET MVC的请求生命周期
本文的目的旨在详细描述asp.net mvc请求从开始到结束的每一个过程. 我希望能理解在浏览器输入url并敲击回车来请求一个asp.net mvc网站的页面之后发生的任何事情. 为什么需要关心这些? ...
随机推荐
- 属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter
属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter 一.普通的get 和set 属性. 一般的get 和set 属 ...
- poj 2823 单调队列
思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...
- css3 盒模型记
css3 盒模型 css假定每个元素都会生成一个或多个矩形框,这称为元素框.各元素框中心有一个内容区.这个内容区周围有可选的内边距,边框和外边距.这些项之所以被认为是可选的,原因是它们的宽度可以设置为 ...
- CSS3如何实现2D转换和3D转换,他们有何区别
CSS3中2D3D技术的发展,带来了更丰富的视觉效果~他们的实现机理是怎样的呢? 1定义 2D: 能够对元素进行移动,缩放,转动,拉长或拉伸. 3D: 允许对元素进行格式化,在三维空间进行操作.元素改 ...
- 一段C程序分析
#include <stdio.h> #include <stdlib.h> void main() { int i; ; char ch; printf("请输入一 ...
- Android对象类系列化public class User implements Parcelable
package com.gaojinhua.android.activitymsg; import android.os.Parcel; import android.os.Parcelable; / ...
- C#中Excel的导入和导出的几种基本方式
在上一篇(http://www.cnblogs.com/fengchengjushi/p/3369386.html)介绍过,Excel也是数据持久化的一种实现方式.在C#中.我们常常会与Excel文件 ...
- (转)设置Win7防火墙规则 顺畅访问局域网
在Windows 7系统的电脑上搭建WAMP环境后,发现在局域网中其他电脑不能访问.有朋友告诉小强,这可能是因为当时Windows 7自带的防火墙屏蔽了80端口,只需要重新设置规则就可以了. 点击Wi ...
- 《JS高级程序设计》笔记 —— 解析查询字符串
今天在继续翻阅<JS高级程序设计>的时候,正好翻到location对象这一小节,其中有一部分就是讲的解析查询字符串.看到这个内容立马想到了做去哪儿秋招笔试题的时候有这么一道题. 去哪儿笔试 ...
- 算法 fill
fill(vec.begin(), vec.end(),); //reset all elements to fill(vec.begin(),vec.begin + vec.size/, );//s ...