Files
Umbraco-CMS/src/Umbraco.Web.Website/Controllers/SurfaceController.cs

106 lines
4.7 KiB
C#
Raw Normal View History

2020-12-08 16:33:50 +11:00
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
2021-02-10 11:42:04 +01:00
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Cms.Web.Website.ActionResults;
2020-05-11 20:29:38 +02:00
namespace Umbraco.Cms.Web.Website.Controllers
2020-05-11 20:29:38 +02:00
{
/// <summary>
/// Provides a base class for front-end add-in controllers.
/// </summary>
[AutoValidateAntiforgeryToken]
2020-05-11 20:29:38 +02:00
public abstract class SurfaceController : PluginController
{
/// <summary>
/// Initializes a new instance of the <see cref="SurfaceController"/> class.
/// </summary>
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
{
UmbracoRouteValues umbracoRouteValues = HttpContext.Features.Get<UmbracoRouteValues>();
if (umbracoRouteValues is null)
2020-12-10 18:09:32 +11:00
{
throw new InvalidOperationException($"No {nameof(UmbracoRouteValues)} feature was found in the HttpContext");
2020-12-10 18:09:32 +11:00
}
2020-05-11 20:29:38 +02:00
return umbracoRouteValues.PublishedRequest.PublishedContent;
2020-05-11 20:29:38 +02:00
}
}
/// <summary>
/// Redirects to the Umbraco page with the given id
/// </summary>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(Guid contentKey)
=> 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>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(Guid contentKey, QueryString queryString)
=> 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>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent)
=> 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>
protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent, QueryString queryString)
=> new RedirectToUmbracoPageResult(publishedContent, queryString, PublishedUrlProvider, UmbracoContextAccessor);
2020-05-11 20:29:38 +02:00
/// <summary>
/// Redirects to the currently rendered Umbraco page
/// </summary>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage()
=> 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>
protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage(QueryString queryString)
=> new RedirectToUmbracoPageResult(CurrentPage, queryString, PublishedUrlProvider, UmbracoContextAccessor);
2020-05-11 20:29:38 +02:00
/// <summary>
/// Redirects to the currently rendered Umbraco URL
/// </summary>
/// <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()
=> new RedirectToUmbracoUrlResult(UmbracoContext);
2020-05-11 20:29:38 +02:00
/// <summary>
/// Returns the currently rendered Umbraco page
/// </summary>
protected UmbracoPageResult CurrentUmbracoPage()
{
2021-02-26 16:53:53 +11:00
HttpContext.Features.Set(new ProxyViewDataFeature(ViewData, TempData));
return new UmbracoPageResult(ProfilingLogger);
}
2020-05-11 20:29:38 +02:00
}
}