ASP.NET中HttpApplication中ProcessRequest方法中运行的事件顺序
1.BeginRequest 開始处理请求
2.AuthenticateRequest 授权验证请求,获取用户授权信息3.PostAuthenticateRequest 获取成功
4.AunthorizeRequest 授权,一般来检查用户是否获得权限
5.PostAuthorizeRequest 获得授权
6.ResolveRequestCache 获取页面缓存结果(假设没有则运行)
7.PostResolveRequestCache 已获取缓存
8.PostMapRequestHandler 创建页面对象
9.AcquireRequestState 获取Session -- 先推断当前页面对象是否实现了IRequiresSessionState接口,假设实现了,则从浏览器发来的请求报文头中获得SessionId,并到server的 Session池中获得相应的Session对象,最后赋值给 HttpContext的Session属性。
10.PostAcquireRequestState 获得Session
11.PreRequestHandlerExecute 准备运行页面对象
×运行页面对象的ProcessRequest方法(1.ashx,1.aspx)。假设请求的是1.aspx。则会运行页面生命周期
12.PostRequestHandlerExecute 运行完页面对象了
13.ReleaseRequestState 释放请求状态
14.PostReleaseRequestState 已释放请求状态
15.UpdateReuqestCache 更新缓存
16.PostUpdateRequestCache 已更新缓存
17.LogRequest 日志记录
18.PostLogRequest 已完毕日志
19.EndRequest 完毕
能够再Global.asax中加入以Application_为前缀+事件名 的方法运行測试!比如:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication obj = sender as HttpApplication;
obj.Context.Response.Write("1 Application_BeginRequest<br/>");
}
public void Application_ResolveRequestCache(object sender, EventArgs e)
{
HttpApplication obj = sender as HttpApplication;
obj.Context.Response.Write("6 Application_ResolveRequestCache<br/>");
}
public void Application_PostResolveRequestCache(object sender, EventArgs e)
{
HttpApplication obj = sender as HttpApplication;
obj.Context.Response.Write("7 Application_PostResolveRequestCache<br/>");
}
public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication obj = sender as HttpApplication;
obj.Context.Response.Write("11 Application_PreRequestHandlerExecute<br/>");
}
}
}
下面附上MVC总体请求流程图。来自传智播客黑马培训班
ASP.NET WebForm总体请求流程图,来自传智播客黑马培训班