[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(三)

时间:2023-03-09 16:13:50
[译] 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 元素。

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(三)

图 1 - 展示生命周期事件详情

提示:这种技术只能使用在排在 PreRequestHandlerExecute 事件之前的事件之上,因为 controller 中的 action 方法会在 PreRequestHandlerExecute 和 PostRequestHandlerExecute 事件之间执行,所以后续触发的事件都已经在响应生成好之后发生了。

[根据 Adam Freeman – Pro ASP.NET MVC 5 Platform 选译]