/src/Umbraco.Web/Mvc/UmbracoPageResult.cs

https://bitbucket.org/jkarsrud/umbracocms-belleui · C# · 53 lines · 35 code · 9 blank · 9 comment · 1 complexity · ae8f623327275c9e4ed1f24180eab557 MD5 · raw file

  1. using System;
  2. using System.Web.Mvc;
  3. using Umbraco.Core;
  4. namespace Umbraco.Web.Mvc
  5. {
  6. /// <summary>
  7. /// Used by posted forms to proxy the result to the page in which the current URL matches on
  8. /// </summary>
  9. public class UmbracoPageResult : ActionResult
  10. {
  11. public override void ExecuteResult(ControllerContext context)
  12. {
  13. //since we could be returning the current page from a surface controller posted values in which the routing values are changed, we
  14. //need to revert these values back to nothing in order for the normal page to render again.
  15. context.RouteData.DataTokens["area"] = null;
  16. context.RouteData.DataTokens["Namespaces"] = null;
  17. //validate that the current page execution is not being handled by the normal umbraco routing system
  18. if (!context.RouteData.DataTokens.ContainsKey("umbraco-route-def"))
  19. {
  20. throw new InvalidOperationException("Can only use " + typeof(UmbracoPageResult).Name + " in the context of an Http POST when using a SurfaceController form");
  21. }
  22. var routeDef = (RouteDefinition)context.RouteData.DataTokens["umbraco-route-def"];
  23. //ensure the original template is reset
  24. context.RouteData.Values["action"] = routeDef.ActionName;
  25. //ensure ModelState is copied across
  26. routeDef.Controller.ViewData.ModelState.Merge(context.Controller.ViewData.ModelState);
  27. //ensure TempData and ViewData is copied across
  28. foreach (var d in context.Controller.ViewData)
  29. routeDef.Controller.ViewData[d.Key] = d.Value;
  30. routeDef.Controller.TempData = context.Controller.TempData;
  31. using (DisposableTimer.TraceDuration<UmbracoPageResult>("Executing Umbraco RouteDefinition controller", "Finished"))
  32. {
  33. try
  34. {
  35. ((IController)routeDef.Controller).Execute(context.RequestContext);
  36. }
  37. finally
  38. {
  39. routeDef.Controller.DisposeIfDisposable();
  40. }
  41. }
  42. }
  43. }
  44. }