2020-10-15 11:42:16 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-01-06 20:03:49 +11:00
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2020-10-15 11:42:16 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
|
|
|
|
using Umbraco.Cms.Web.Common.ActionsResults;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Filters;
|
2021-08-17 11:41:28 +02:00
|
|
|
using Umbraco.Extensions;
|
2020-10-15 11:42:16 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the default front-end rendering controller.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ModelBindingException]
|
|
|
|
|
[PublishedRequestFilter]
|
2022-12-14 08:14:19 +01:00
|
|
|
[MaintenanceModeActionFilter]
|
2022-05-09 09:39:46 +02:00
|
|
|
public class RenderController : UmbracoPageController, IRenderController
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly ILogger<RenderController> _logger;
|
|
|
|
|
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
2021-02-15 18:50:16 +11:00
|
|
|
|
2020-10-15 11:42:16 +02:00
|
|
|
/// <summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
/// Initializes a new instance of the <see cref="RenderController" /> class.
|
2020-10-15 11:42:16 +02:00
|
|
|
/// </summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
public RenderController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor)
|
|
|
|
|
: base(logger, compositeViewEngine)
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
_logger = logger;
|
|
|
|
|
_umbracoContextAccessor = umbracoContextAccessor;
|
|
|
|
|
}
|
2020-10-15 11:42:16 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the umbraco context
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected IUmbracoContext UmbracoContext
|
|
|
|
|
{
|
|
|
|
|
get
|
2021-08-11 12:17:35 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
|
|
|
|
return umbracoContext;
|
2021-08-11 12:17:35 +02:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
2020-10-15 11:42:16 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// The default action to render the front-end view.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual IActionResult Index() => CurrentTemplate(new ContentModel(CurrentPage));
|
2022-01-18 15:36:25 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Before the controller executes we will handle redirects and not founds
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
|
|
|
{
|
|
|
|
|
IPublishedRequest pcr = UmbracoRouteValues.PublishedRequest;
|
2023-06-07 21:47:05 +12:00
|
|
|
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug(
|
2022-05-09 09:39:46 +02:00
|
|
|
"Response status: Content={Content}, StatusCode={ResponseStatusCode}, Culture={Culture}",
|
|
|
|
|
pcr.PublishedContent?.Id ?? -1,
|
|
|
|
|
pcr.ResponseStatusCode,
|
|
|
|
|
pcr.Culture);
|
2023-06-07 21:47:05 +12:00
|
|
|
}
|
2022-01-18 15:36:25 +01:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
UmbracoRouteResult routeStatus = pcr.GetRouteResult();
|
|
|
|
|
switch (routeStatus)
|
2021-01-06 20:03:49 +11:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
case UmbracoRouteResult.Redirect:
|
|
|
|
|
|
|
|
|
|
// set the redirect result and do not call next to short circuit
|
|
|
|
|
context.Result = pcr.IsRedirectPermanent()
|
|
|
|
|
? RedirectPermanent(pcr.RedirectUrl!)
|
|
|
|
|
: Redirect(pcr.RedirectUrl!);
|
|
|
|
|
break;
|
|
|
|
|
case UmbracoRouteResult.NotFound:
|
|
|
|
|
// set the redirect result and do not call next to short circuit
|
|
|
|
|
context.Result = GetNoTemplateResult(pcr);
|
|
|
|
|
break;
|
|
|
|
|
case UmbracoRouteResult.Success:
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
// Check if there's a ProxyViewDataFeature in the request.
|
|
|
|
|
// If there it is means that we are proxying/executing this controller
|
|
|
|
|
// from another controller and we need to merge it's ViewData with this one
|
|
|
|
|
// since this one will be empty.
|
|
|
|
|
ProxyViewDataFeature? saveViewData = HttpContext.Features.Get<ProxyViewDataFeature>();
|
|
|
|
|
if (saveViewData != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<string, object?> kv in saveViewData.ViewData)
|
2021-02-04 14:51:55 +11:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
ViewData[kv.Key] = kv.Value;
|
2021-02-04 14:51:55 +11:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// continue normally
|
|
|
|
|
await next();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an action result based on the template name found in the route values and a model.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type of the model.</typeparam>
|
|
|
|
|
/// <param name="model">The model.</param>
|
|
|
|
|
/// <returns>The action result.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If the template found in the route values doesn't physically exist, Umbraco not found result is returned.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
protected override IActionResult CurrentTemplate<T>(T model)
|
|
|
|
|
{
|
|
|
|
|
if (EnsurePhsyicalViewExists(UmbracoRouteValues.TemplateName) == false)
|
|
|
|
|
{
|
|
|
|
|
// no physical template file was found
|
|
|
|
|
return new PublishedContentNotFoundResult(UmbracoContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(UmbracoRouteValues.TemplateName, model);
|
|
|
|
|
}
|
2021-02-04 14:51:55 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
private PublishedContentNotFoundResult GetNoTemplateResult(IPublishedRequest pcr)
|
|
|
|
|
{
|
|
|
|
|
// missing template, so we're in a 404 here
|
|
|
|
|
// so the content, if any, is a custom 404 page of some sort
|
|
|
|
|
if (!pcr.HasPublishedContent())
|
|
|
|
|
{
|
|
|
|
|
// means the builder could not find a proper document to handle 404
|
|
|
|
|
return new PublishedContentNotFoundResult(UmbracoContext);
|
2021-01-06 20:03:49 +11:00
|
|
|
}
|
2021-01-08 02:10:13 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
if (!pcr.HasTemplate())
|
2021-01-08 02:10:13 +11:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
// means the engine could find a proper document, but the document has no template
|
|
|
|
|
// at that point there isn't much we can do
|
|
|
|
|
return new PublishedContentNotFoundResult(
|
|
|
|
|
UmbracoContext,
|
|
|
|
|
"In addition, no template exists to render the custom 404.");
|
2022-01-18 15:36:25 +01:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
|
|
|
|
|
return new PublishedContentNotFoundResult(UmbracoContext);
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
}
|