Go to the documentation of this file.00001 using System;
00002 using System.Web.Mvc;
00003 using System.Web.Routing;
00004
00005 namespace N2.Web.Mvc
00006 {
00010 public class ViewPageResult : ActionResult
00011 {
00012 private readonly ContentItem _thePage;
00013 private readonly IControllerMapper _controllerMapper;
00014 private readonly IWebContext _webContext;
00015 private readonly IActionInvoker _actionInvoker;
00016
00017 public ViewPageResult(ContentItem thePage, IControllerMapper controllerMapper, IWebContext webContext, IActionInvoker actionInvoker)
00018 {
00019 _thePage = thePage;
00020 _controllerMapper = controllerMapper;
00021 _webContext = webContext;
00022 _actionInvoker = actionInvoker;
00023 }
00024
00025 public ContentItem Page
00026 {
00027 get { return _thePage; }
00028 }
00029
00034 public override void ExecuteResult(ControllerContext context)
00035 {
00036 SetupN2ForNewPageRequest();
00037
00038 context = BuildPageControllerContext(context);
00039
00040 _actionInvoker.InvokeAction(context, "Index");
00041 }
00042
00043 private void SetupN2ForNewPageRequest()
00044 {
00045 _webContext.CurrentPage = _thePage;
00046 }
00047
00048 private ControllerContext BuildPageControllerContext(ControllerContext context)
00049 {
00050 string controllerName = _controllerMapper.GetControllerName(_thePage.GetContentType());
00051
00052 var routeData = context.RouteData;
00053 routeData.ApplyCurrentItem(controllerName, "Index", _thePage, null);
00054 if (context.RouteData.DataTokens.ContainsKey(ContentRoute.ContentPartKey))
00055 {
00056 routeData.ApplyContentItem(ContentRoute.ContentPartKey, context.RouteData.DataTokens[ContentRoute.ContentPartKey] as ContentItem);
00057 }
00058
00059 var requestContext = new RequestContext(context.HttpContext, routeData);
00060
00061 var controller = (ControllerBase)ControllerBuilder.Current.GetControllerFactory()
00062 .CreateController(requestContext, controllerName);
00063
00064 controller.ControllerContext = new ControllerContext(requestContext, controller);
00065 controller.ViewData.ModelState.Merge(context.Controller.ViewData.ModelState);
00066
00067 return controller.ControllerContext;
00068 }
00069 }
00070 }