- public class MvcHandler : IHttpHandler
- {
- public RequestContext RequestContext { get; private set; }
- public bool IsReusable
- {
- get { return false; }
- }
- public MvcHandler(RequestContext requestContext)
- {
- this.RequestContext = requestContext;
- }
- public void ProcessRequest(HttpContext context)
- {
- IControllerFactory controllerFactory = ControllerBuilder.Current.GetControllerFactory();
- string controllerName = this.RequestContext.RouteData.GetRequiredString("controller");
- IController controller = controllerFactory.CreateController(this.RequestContext, controllerName);
- try
- {
- controller.Execute(this.RequestContext);
- }
- finally
- {
- controllerFactory.ReleaseController(controller);
- }
- }
- : }
在ProcessRequest方法中,通过RequestContext对象得到目标Controller的名称,并通过它利用当前ControllerBuilder创建的ControllerFactory激活Controller对象。在执行了被激活Controller对象的Execute方法之后调用ControllerFactory的ReleaseController对其进行释放清理工作。
原文链接:http://www.cnblogs.com/artech/archive/2012/03/31/controller-activation-01.html