2020-05-11 20:29:38 +02:00
using System ;
using System.Collections.Specialized ;
2020-08-05 08:44:22 +02:00
using Microsoft.AspNetCore.Http ;
2020-05-11 20:29:38 +02:00
using Umbraco.Core ;
using Umbraco.Core.Cache ;
using Umbraco.Core.Logging ;
using Umbraco.Core.Models.PublishedContent ;
using Umbraco.Core.Persistence ;
using Umbraco.Core.Services ;
using Umbraco.Web.Common.Controllers ;
using Umbraco.Web.Routing ;
using Umbraco.Web.Website.ActionResults ;
namespace Umbraco.Web.Website.Controllers
{
/// <summary>
/// Provides a base class for front-end add-in controllers.
/// </summary>
2020-05-12 20:28:40 +02:00
// TODO: Migrate MergeModelStateToChildAction and MergeParentContextViewData action filters
// [MergeModelStateToChildAction]
// [MergeParentContextViewData]
2020-05-11 20:29:38 +02:00
public abstract class SurfaceController : PluginController
{
2020-11-18 16:12:42 +01:00
protected SurfaceController ( IUmbracoContextAccessor umbracoContextAccessor , IUmbracoDatabaseFactory databaseFactory , ServiceContext services , AppCaches appCaches , IProfilingLogger profilingLogger , IPublishedUrlProvider publishedUrlProvider )
: base ( umbracoContextAccessor , databaseFactory , services , appCaches , profilingLogger )
{
PublishedUrlProvider = publishedUrlProvider ;
}
protected IPublishedUrlProvider PublishedUrlProvider { get ; }
2020-05-11 20:29:38 +02:00
/// <summary>
/// Gets the current page.
/// </summary>
protected virtual IPublishedContent CurrentPage
{
get
{
var routeDefAttempt = TryGetRouteDefinitionFromAncestorViewContexts ( ) ;
if ( routeDefAttempt . Success = = false )
throw routeDefAttempt . Exception ;
var routeDef = routeDefAttempt . Result ;
return routeDef . PublishedRequest . PublishedContent ;
}
}
/// <summary>
/// Redirects to the Umbraco page with the given id
/// </summary>
2020-11-12 11:01:19 +01:00
/// <param name="contentKey"></param>
2020-05-11 20:29:38 +02:00
/// <returns></returns>
2020-11-12 11:01:19 +01:00
protected RedirectToUmbracoPageResult RedirectToUmbracoPage ( Guid contentKey )
2020-05-11 20:29:38 +02:00
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( contentKey , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id and passes provided querystring
/// </summary>
2020-11-12 11:01:19 +01:00
/// <param name="contentKey"></param>
2020-05-11 20:29:38 +02:00
/// <param name="queryString"></param>
/// <returns></returns>
2020-11-12 11:01:19 +01:00
protected RedirectToUmbracoPageResult RedirectToUmbracoPage ( Guid contentKey , QueryString queryString )
2020-05-11 20:29:38 +02:00
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( contentKey , queryString , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given published content
/// </summary>
/// <param name="publishedContent"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage ( IPublishedContent publishedContent )
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( publishedContent , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given published content and passes provided querystring
/// </summary>
/// <param name="publishedContent"></param>
/// <param name="queryString"></param>
/// <returns></returns>
2020-08-05 08:44:22 +02:00
protected RedirectToUmbracoPageResult RedirectToUmbracoPage ( IPublishedContent publishedContent , QueryString queryString )
2020-05-11 20:29:38 +02:00
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( publishedContent , queryString , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco page
/// </summary>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage ( )
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( CurrentPage , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco page and passes provided querystring
/// </summary>
/// <param name="queryString"></param>
/// <returns></returns>
2020-08-05 08:44:22 +02:00
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage ( QueryString queryString )
2020-05-11 20:29:38 +02:00
{
2020-11-18 16:12:42 +01:00
return new RedirectToUmbracoPageResult ( CurrentPage , queryString , PublishedUrlProvider , UmbracoContextAccessor ) ;
2020-05-11 20:29:38 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco URL
/// </summary>
/// <returns></returns>
/// <remarks>
/// This is useful if you need to redirect
/// to the current page but the current page is actually a rewritten URL normally done with something like
/// Server.Transfer.*
/// </remarks>
protected RedirectToUmbracoUrlResult RedirectToCurrentUmbracoUrl ( )
{
return new RedirectToUmbracoUrlResult ( UmbracoContext ) ;
}
/// <summary>
/// Returns the currently rendered Umbraco page
/// </summary>
/// <returns></returns>
protected UmbracoPageResult CurrentUmbracoPage ( )
{
return new UmbracoPageResult ( ProfilingLogger ) ;
}
/// <summary>
/// we need to recursively find the route definition based on the parent view context
/// </summary>
/// <returns></returns>
private Attempt < RouteDefinition > TryGetRouteDefinitionFromAncestorViewContexts ( )
{
var currentContext = ControllerContext ;
while ( ! ( currentContext is null ) )
{
var currentRouteData = currentContext . RouteData ;
if ( currentRouteData . DataTokens . ContainsKey ( Core . Constants . Web . UmbracoRouteDefinitionDataToken ) )
return Attempt . Succeed ( ( RouteDefinition ) currentRouteData . DataTokens [ Core . Constants . Web . UmbracoRouteDefinitionDataToken ] ) ;
}
return Attempt < RouteDefinition > . Fail (
new InvalidOperationException ( "Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request" ) ) ;
}
}
}