Files
Umbraco-CMS/src/Umbraco.Web/Mvc/SurfaceController.cs

189 lines
7.7 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using Umbraco.Core;
using System.Collections.Specialized;
2018-07-06 17:36:33 +02:00
using Umbraco.Core.Cache;
using Umbraco.Core.Logging;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Models.PublishedContent;
2018-07-06 17:36:33 +02:00
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Composing;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Web.Mvc
{
/// <summary>
/// Provides a base class for front-end add-in controllers.
/// </summary>
2020-05-11 20:29:38 +02:00
/// Migrated already to .Net Core without MergeModelStateToChildAction and MergeParentContextViewData action filters
/// TODO: Migrate MergeModelStateToChildAction and MergeParentContextViewData action filters
2018-06-29 19:52:40 +02:00
[MergeModelStateToChildAction]
[MergeParentContextViewData]
public abstract class SurfaceController : PluginController
{
protected SurfaceController()
2019-01-21 15:57:48 +01:00
{ }
2018-07-06 17:36:33 +02:00
protected SurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, ILogger logger, IProfilingLogger profilingLogger)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, logger, profilingLogger)
2019-01-21 15:57:48 +01:00
{ }
2018-07-06 17:36:33 +02:00
2018-06-29 19:52:40 +02:00
/// <summary>
/// Redirects to the Umbraco page with the given id
/// </summary>
/// <param name="pageId"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId)
{
return new RedirectToUmbracoPageResult(pageId, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id and passes provided querystring
/// </summary>
/// <param name="pageId"></param>
/// <param name="queryStringValues"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId, NameValueCollection queryStringValues)
{
return new RedirectToUmbracoPageResult(pageId, queryStringValues, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id and passes provided querystring
/// </summary>
/// <param name="pageId"></param>
/// <param name="queryString"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId, string queryString)
{
return new RedirectToUmbracoPageResult(pageId, queryString, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id
/// </summary>
/// <param name="publishedContent"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent)
{
return new RedirectToUmbracoPageResult(publishedContent, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id and passes provided querystring
/// </summary>
/// <param name="publishedContent"></param>
/// <param name="queryStringValues"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent, NameValueCollection queryStringValues)
{
return new RedirectToUmbracoPageResult(publishedContent, queryStringValues, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the Umbraco page with the given id and passes provided querystring
/// </summary>
/// <param name="publishedContent"></param>
/// <param name="queryString"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent, string queryString)
{
return new RedirectToUmbracoPageResult(publishedContent, queryString, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco page
/// </summary>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage()
{
return new RedirectToUmbracoPageResult(CurrentPage, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco page and passes provided querystring
/// </summary>
/// <param name="queryStringValues"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage(NameValueCollection queryStringValues)
{
return new RedirectToUmbracoPageResult(CurrentPage, queryStringValues, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// Redirects to the currently rendered Umbraco page and passes provided querystring
/// </summary>
/// <param name="queryString"></param>
/// <returns></returns>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage(string queryString)
{
return new RedirectToUmbracoPageResult(CurrentPage, queryString, Current.PublishedUrlProvider);
2018-06-29 19:52:40 +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>
/// 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>
/// we need to recursively find the route definition based on the parent view context
/// </summary>
/// <returns></returns>
/// <remarks>
/// We may have Child Actions within Child actions so we need to recursively look this up.
/// see: http://issues.umbraco.org/issue/U4-1844
/// </remarks>
private Attempt<RouteDefinition> TryGetRouteDefinitionFromAncestorViewContexts()
{
var currentContext = ControllerContext;
while (currentContext != null)
{
var currentRouteData = currentContext.RouteData;
if (currentRouteData.DataTokens.ContainsKey(Core.Constants.Web.UmbracoRouteDefinitionDataToken))
return Attempt.Succeed((RouteDefinition)currentRouteData.DataTokens[Core.Constants.Web.UmbracoRouteDefinitionDataToken]);
currentContext = currentContext.IsChildAction
? currentContext.ParentActionViewContext
: null;
}
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"));
}
}
}