using System;
using System.Collections.Concurrent;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Models;
using Umbraco.Core;
namespace Umbraco.Web.Mvc
{
///
/// The base controller that all Presentation Add-in controllers should inherit from
///
[MergeModelStateToChildAction]
public abstract class SurfaceController : PluginController
{
///
/// Default constructor
///
///
protected SurfaceController(UmbracoContext umbracoContext)
: base(umbracoContext)
{
}
///
/// Empty constructor, uses Singleton to resolve the UmbracoContext
///
protected SurfaceController()
: base(UmbracoContext.Current)
{
}
///
/// Redirects to the Umbraco page with the given id
///
///
///
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId)
{
return new RedirectToUmbracoPageResult(pageId, UmbracoContext);
}
///
/// Redirects to the Umbraco page with the given id
///
///
///
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent)
{
return new RedirectToUmbracoPageResult(publishedContent, UmbracoContext);
}
///
/// Redirects to the currently rendered Umbraco page
///
///
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage()
{
return new RedirectToUmbracoPageResult(CurrentPage, UmbracoContext);
}
///
/// Returns the currently rendered Umbraco page
///
///
protected UmbracoPageResult CurrentUmbracoPage()
{
return new UmbracoPageResult();
}
///
/// Gets the current page.
///
protected IPublishedContent CurrentPage
{
get
{
var routeData = ControllerContext.IsChildAction
? ControllerContext.ParentActionViewContext.RouteData
: ControllerContext.RouteData;
if (!routeData.DataTokens.ContainsKey("umbraco-route-def"))
{
throw new InvalidOperationException("Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request");
}
var routeDef = (RouteDefinition)routeData.DataTokens["umbraco-route-def"];
return routeDef.PublishedContentRequest.PublishedContent;
}
}
}
}