Files
Umbraco-CMS/src/Umbraco.Web.Common/Controllers/RenderController.cs

100 lines
3.6 KiB
C#
Raw Normal View History

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.Extensions.Logging;
2020-12-10 18:09:32 +11:00
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.Common.Filters;
2020-12-10 18:09:32 +11:00
using Umbraco.Web.Common.Routing;
using Umbraco.Web.Models;
using Umbraco.Web.Routing;
2020-12-10 18:09:32 +11:00
namespace Umbraco.Web.Common.Controllers
{
/// <summary>
/// Represents the default front-end rendering controller.
/// </summary>
2020-12-22 10:52:25 +11:00
[ModelBindingException]
2020-12-08 16:33:50 +11:00
public class RenderController : UmbracoController, IRenderController
{
2020-12-08 16:33:50 +11:00
private readonly ILogger<RenderController> _logger;
private readonly ICompositeViewEngine _compositeViewEngine;
2020-12-22 10:52:25 +11:00
private UmbracoRouteValues _umbracoRouteValues;
2020-12-08 16:33:50 +11:00
/// <summary>
/// Initializes a new instance of the <see cref="RenderController"/> class.
/// </summary>
public RenderController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine)
{
_logger = logger;
_compositeViewEngine = compositeViewEngine;
}
/// <summary>
/// Gets the current content item.
/// </summary>
2020-12-10 18:09:32 +11:00
protected IPublishedContent CurrentPage => UmbracoRouteValues.PublishedContent;
/// <summary>
2020-12-10 18:09:32 +11:00
/// Gets the <see cref="UmbracoRouteValues"/>
/// </summary>
2020-12-10 18:09:32 +11:00
protected UmbracoRouteValues UmbracoRouteValues
{
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
}
2020-12-10 18:09:32 +11:00
if (!ControllerContext.RouteData.Values.TryGetValue(Core.Constants.Web.UmbracoRouteDefinitionDataToken, out var def))
{
2020-12-10 18:09:32 +11:00
throw new InvalidOperationException($"No route value found with key {Core.Constants.Web.UmbracoRouteDefinitionDataToken}");
}
2020-12-08 16:33:50 +11:00
2020-12-22 10:52:25 +11:00
_umbracoRouteValues = (UmbracoRouteValues)def;
return _umbracoRouteValues;
}
}
/// <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;
}
_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>
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);
}
/// <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));
}
}