removes ResponseStatusDescription and others that aren't used, ports the not found handler, ports redirects, headers, etc...
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Umbraco.Web.Common.Routing;
|
||||
using Umbraco.Web.Routing;
|
||||
|
||||
namespace Umbraco.Web.Common.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Deals with custom headers for the umbraco request
|
||||
/// </summary>
|
||||
internal class PublishedRequestFilterAttribute : ResultFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="UmbracoRouteValues"/>
|
||||
/// </summary>
|
||||
protected UmbracoRouteValues GetUmbracoRouteValues(ResultExecutingContext context)
|
||||
{
|
||||
if (!context.RouteData.Values.TryGetValue(Core.Constants.Web.UmbracoRouteDefinitionDataToken, out var def))
|
||||
{
|
||||
throw new InvalidOperationException($"No route value found with key {Core.Constants.Web.UmbracoRouteDefinitionDataToken}");
|
||||
}
|
||||
|
||||
return (UmbracoRouteValues)def;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deals with custom headers for the umbraco request
|
||||
/// </summary>
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
UmbracoRouteValues routeVals = GetUmbracoRouteValues(context);
|
||||
IPublishedRequest pcr = routeVals.PublishedRequest;
|
||||
|
||||
// now we can deal with headers, etc...
|
||||
if (pcr.ResponseStatusCode.HasValue)
|
||||
{
|
||||
// set status code -- even for redirects
|
||||
context.HttpContext.Response.StatusCode = pcr.ResponseStatusCode.Value;
|
||||
}
|
||||
|
||||
AddCacheControlHeaders(context, pcr);
|
||||
|
||||
if (pcr.Headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> header in pcr.Headers)
|
||||
{
|
||||
context.HttpContext.Response.Headers.Append(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddCacheControlHeaders(ResultExecutingContext context, IPublishedRequest pcr)
|
||||
{
|
||||
var cacheControlHeaders = new List<string>();
|
||||
|
||||
if (pcr.CacheabilityNoCache)
|
||||
{
|
||||
cacheControlHeaders.Add("no-cache");
|
||||
}
|
||||
|
||||
if (pcr.CacheExtensions != null)
|
||||
{
|
||||
foreach (var cacheExtension in pcr.CacheExtensions)
|
||||
{
|
||||
cacheControlHeaders.Add(cacheExtension);
|
||||
}
|
||||
}
|
||||
|
||||
if (cacheControlHeaders.Count > 0)
|
||||
{
|
||||
context.HttpContext.Response.Headers["Cache-Control"] = string.Join(", ", cacheControlHeaders);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Web.Common.ActionsResults;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
using Umbraco.Web.Common.Routing;
|
||||
using Umbraco.Web.Models;
|
||||
@@ -11,30 +13,50 @@ using Umbraco.Web.Routing;
|
||||
|
||||
namespace Umbraco.Web.Common.Controllers
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Represents the default front-end rendering controller.
|
||||
/// </summary>
|
||||
[ModelBindingException]
|
||||
[PublishedRequestFilter]
|
||||
public class RenderController : UmbracoController, IRenderController
|
||||
{
|
||||
private readonly ILogger<RenderController> _logger;
|
||||
private readonly ICompositeViewEngine _compositeViewEngine;
|
||||
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
||||
private UmbracoRouteValues _umbracoRouteValues;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RenderController"/> class.
|
||||
/// </summary>
|
||||
public RenderController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine)
|
||||
public RenderController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor)
|
||||
{
|
||||
_logger = logger;
|
||||
_compositeViewEngine = compositeViewEngine;
|
||||
_umbracoContextAccessor = umbracoContextAccessor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current content item.
|
||||
/// </summary>
|
||||
protected IPublishedContent CurrentPage => UmbracoRouteValues.PublishedContent;
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="UmbracoRouteValues"/>
|
||||
@@ -95,5 +117,41 @@ namespace Umbraco.Web.Common.Controllers
|
||||
/// The default action to render the front-end view.
|
||||
/// </summary>
|
||||
public virtual IActionResult Index() => CurrentTemplate(new ContentModel(CurrentPage));
|
||||
|
||||
/// <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(
|
||||
"Response status: Redirect={Redirect}, Is404={Is404}, StatusCode={ResponseStatusCode}",
|
||||
pcr.IsRedirect() ? (pcr.IsRedirectPermanent() ? "permanent" : "redirect") : "none",
|
||||
pcr.Is404() ? "true" : "false",
|
||||
pcr.ResponseStatusCode);
|
||||
|
||||
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
|
||||
context.Result = new PublishedContentNotFoundResult(UmbracoContext);
|
||||
break;
|
||||
case UmbracoRouteResult.Success:
|
||||
default:
|
||||
// continue normally
|
||||
await next();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user