2020-10-15 11:42:16 +02:00
|
|
|
using System;
|
2021-01-06 20:03:49 +11:00
|
|
|
using System.Threading.Tasks;
|
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;
|
2021-01-08 11:29:07 +11:00
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-10-15 11:42:16 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2021-01-06 20:03:49 +11:00
|
|
|
using Umbraco.Web.Common.ActionsResults;
|
2020-10-15 11:42:16 +02:00
|
|
|
using Umbraco.Web.Common.Filters;
|
2020-12-10 18:09:32 +11:00
|
|
|
using Umbraco.Web.Common.Routing;
|
2020-10-15 11:42:16 +02:00
|
|
|
using Umbraco.Web.Models;
|
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
2020-12-10 18:09:32 +11:00
|
|
|
namespace Umbraco.Web.Common.Controllers
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the default front-end rendering controller.
|
|
|
|
|
/// </summary>
|
2020-12-22 10:52:25 +11:00
|
|
|
[ModelBindingException]
|
2021-01-06 20:03:49 +11:00
|
|
|
[PublishedRequestFilter]
|
2020-12-08 16:33:50 +11:00
|
|
|
public class RenderController : UmbracoController, IRenderController
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
private readonly ILogger<RenderController> _logger;
|
2020-10-15 11:42:16 +02:00
|
|
|
private readonly ICompositeViewEngine _compositeViewEngine;
|
2021-01-06 20:03:49 +11:00
|
|
|
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
2020-12-22 10:52:25 +11:00
|
|
|
private UmbracoRouteValues _umbracoRouteValues;
|
2020-10-15 11:42:16 +02:00
|
|
|
|
2020-12-08 16:33:50 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RenderController"/> class.
|
|
|
|
|
/// </summary>
|
2021-01-06 20:03:49 +11:00
|
|
|
public RenderController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor)
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_compositeViewEngine = compositeViewEngine;
|
2021-01-06 20:03:49 +11:00
|
|
|
_umbracoContextAccessor = umbracoContextAccessor;
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current content item.
|
|
|
|
|
/// </summary>
|
2021-01-06 20:03:49 +11:00
|
|
|
protected IPublishedContent CurrentPage
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!UmbracoRouteValues.PublishedRequest.HasPublishedContent())
|
|
|
|
|
{
|
|
|
|
|
// This will never be accessed this way since the controller will handle redirects and not founds
|
|
|
|
|
// before this can be accessed but we need to be explicit.
|
|
|
|
|
throw new InvalidOperationException("There is no published content found in the request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return UmbracoRouteValues.PublishedRequest.PublishedContent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the umbraco context
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected IUmbracoContext UmbracoContext => _umbracoContextAccessor.UmbracoContext;
|
2020-10-15 11:42:16 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2020-12-10 18:09:32 +11:00
|
|
|
/// Gets the <see cref="UmbracoRouteValues"/>
|
2020-10-15 11:42:16 +02:00
|
|
|
/// </summary>
|
2020-12-10 18:09:32 +11:00
|
|
|
protected UmbracoRouteValues UmbracoRouteValues
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-12-22 10:52:25 +11:00
|
|
|
if (_umbracoRouteValues != null)
|
2020-12-08 16:33:50 +11:00
|
|
|
{
|
2020-12-22 10:52:25 +11:00
|
|
|
return _umbracoRouteValues;
|
2020-12-08 16:33:50 +11:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 15:47:27 +11:00
|
|
|
_umbracoRouteValues = HttpContext.Features.Get<UmbracoRouteValues>();
|
2021-01-08 11:29:07 +11:00
|
|
|
|
|
|
|
|
if (_umbracoRouteValues == null)
|
2020-10-15 11:42:16 +02:00
|
|
|
{
|
2021-02-03 15:47:27 +11:00
|
|
|
throw new InvalidOperationException($"No {nameof(UmbracoRouteValues)} feature was found in the HttpContext");
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
2020-12-08 16:33:50 +11:00
|
|
|
|
2020-12-22 10:52:25 +11:00
|
|
|
return _umbracoRouteValues;
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that a physical view file exists on disk.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="template">The view name.</param>
|
|
|
|
|
protected bool EnsurePhsyicalViewExists(string template)
|
|
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
ViewEngineResult result = _compositeViewEngine.FindView(ControllerContext, template, false);
|
|
|
|
|
if (result.View != null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-10-15 11:42:16 +02:00
|
|
|
|
|
|
|
|
_logger.LogWarning("No physical template file was found for template {Template}", template);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2020-12-10 18:09:32 +11:00
|
|
|
/// <exception cref="InvalidOperationException">If the template found in the route values doesn't physically exist and exception is thrown</exception>
|
2020-10-15 11:42:16 +02:00
|
|
|
protected IActionResult CurrentTemplate<T>(T model)
|
|
|
|
|
{
|
2020-12-10 18:09:32 +11:00
|
|
|
if (EnsurePhsyicalViewExists(UmbracoRouteValues.TemplateName) == false)
|
2020-12-08 16:33:50 +11:00
|
|
|
{
|
2020-12-10 18:09:32 +11:00
|
|
|
throw new InvalidOperationException("No physical template file was found for template " + UmbracoRouteValues.TemplateName);
|
2020-12-08 16:33:50 +11:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 18:09:32 +11:00
|
|
|
return View(UmbracoRouteValues.TemplateName, model);
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The default action to render the front-end view.
|
|
|
|
|
/// </summary>
|
2020-12-10 18:09:32 +11:00
|
|
|
public virtual IActionResult Index() => CurrentTemplate(new ContentModel(CurrentPage));
|
2021-01-06 20:03:49 +11: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;
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug(
|
2021-01-08 11:29:07 +11:00
|
|
|
"Response status: Content={Content}, StatusCode={ResponseStatusCode}, Culture={Culture}",
|
|
|
|
|
pcr.PublishedContent?.Id ?? -1,
|
|
|
|
|
pcr.ResponseStatusCode,
|
|
|
|
|
pcr.Culture);
|
2021-01-06 20:03:49 +11:00
|
|
|
|
|
|
|
|
UmbracoRouteResult routeStatus = pcr.GetRouteResult();
|
|
|
|
|
switch (routeStatus)
|
|
|
|
|
{
|
|
|
|
|
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
|
2021-01-08 02:10:13 +11:00
|
|
|
context.Result = GetNoTemplateResult(pcr);
|
2021-01-06 20:03:49 +11:00
|
|
|
break;
|
|
|
|
|
case UmbracoRouteResult.Success:
|
|
|
|
|
default:
|
|
|
|
|
// continue normally
|
|
|
|
|
await next();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-08 02:10:13 +11: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);
|
|
|
|
|
}
|
|
|
|
|
else if (!pcr.HasTemplate())
|
|
|
|
|
{
|
|
|
|
|
// 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.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new PublishedContentNotFoundResult(UmbracoContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-15 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
}
|