Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dotnet_analyzer_diagnostic.category-StyleCop.CSharp.LayoutRules.severity = sugge

dotnet_diagnostic.SA1636.severity = none # SA1636: File header copyright text should match
dotnet_diagnostic.SA1101.severity = none # PrefixLocalCallsWithThis - stylecop appears to be ignoring dotnet_style_qualification_for_*
dotnet_diagnostic.SA1309.severity = none # FieldNamesMustNotBeginWithUnderscore

dotnet_diagnostic.SA1503.severity = warning # BracesMustNotBeOmitted
dotnet_diagnostic.SA1117.severity = warning # ParametersMustBeOnSameLineOrSeparateLines
Expand All @@ -70,13 +71,13 @@ dotnet_diagnostic.SA1132.severity = warning # DoNotCombineFields
dotnet_diagnostic.SA1134.severity = warning # AttributesMustNotShareLine
dotnet_diagnostic.SA1106.severity = warning # CodeMustNotContainEmptyStatements
dotnet_diagnostic.SA1312.severity = warning # VariableNamesMustBeginWithLowerCaseLetter
dotnet_diagnostic.SA1303.severity = warning # ConstFieldNamesMustBeginWithUpperCaseLetter
dotnet_diagnostic.SA1310.severity = warning # FieldNamesMustNotContainUnderscore
dotnet_diagnostic.SA1303.severity = warning # ConstFieldNamesMustBeginWithUpperCaseLetter
dotnet_diagnostic.SA1130.severity = warning # UseLambdaSyntax
dotnet_diagnostic.SA1405.severity = warning # DebugAssertMustProvideMessageText
dotnet_diagnostic.SA1205.severity = warning # PartialElementsMustDeclareAccess
dotnet_diagnostic.SA1306.severity = warning # FieldNamesMustBeginWithLowerCaseLetter
dotnet_diagnostic.SA1209.severity = warning # UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives
dotnet_diagnostic.SA1216.severity = warning # UsingStaticDirectivesMustBePlacedAtTheCorrectLocation
dotnet_diagnostic.SA1133.severity = warning # DoNotCombineAttributes
dotnet_diagnostic.SA1135.severity = warning # UsingDirectivesMustBeQualified
dotnet_diagnostic.SA1135.severity = warning # UsingDirectivesMustBeQualified
10 changes: 7 additions & 3 deletions src/Umbraco.Web.UI/Composers/ControllersAsServicesComposer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Extensions;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;

namespace Umbraco.Cms.Web.UI.Composers
{
Expand Down Expand Up @@ -55,6 +58,7 @@ public static IMvcBuilder AddControllersAsServicesWithoutChangingActivator(this
builder.Services.TryAddTransient(controller, controller);
}

builder.Services.AddUnique<RenderNoContentController>(x => new RenderNoContentController(x.GetService<IUmbracoContextAccessor>()!, x.GetService<IOptionsSnapshot<GlobalSettings>>()!, x.GetService<IHostingEnvironment>()!));
return builder;
}
}
Expand Down
209 changes: 110 additions & 99 deletions src/Umbraco.Web.Website/ActionResults/RedirectToUmbracoPageResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
Expand All @@ -10,130 +8,143 @@
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;

namespace Umbraco.Cms.Web.Website.ActionResults
namespace Umbraco.Cms.Web.Website.ActionResults;

/// <summary>
/// Redirects to an Umbraco page by Id or Entity
/// </summary>
public class RedirectToUmbracoPageResult : IKeepTempDataResult
{
private readonly IPublishedUrlProvider _publishedUrlProvider;
private readonly QueryString _queryString;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private IPublishedContent? _publishedContent;
private string? _url;

/// <summary>
/// Redirects to an Umbraco page by Id or Entity
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult" /> class.
/// </summary>
public class RedirectToUmbracoPageResult : IActionResult, IKeepTempDataResult
public RedirectToUmbracoPageResult(
Guid key,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoContextAccessor umbracoContextAccessor)
{
private IPublishedContent? _publishedContent;
private readonly QueryString _queryString;
private readonly IPublishedUrlProvider _publishedUrlProvider;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private string? _url;

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult"/> class.
/// </summary>
public RedirectToUmbracoPageResult(Guid key, IPublishedUrlProvider publishedUrlProvider, IUmbracoContextAccessor umbracoContextAccessor)
{
Key = key;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}
Key = key;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult"/> class.
/// </summary>
public RedirectToUmbracoPageResult(Guid key, QueryString queryString, IPublishedUrlProvider publishedUrlProvider, IUmbracoContextAccessor umbracoContextAccessor)
{
Key = key;
_queryString = queryString;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult" /> class.
/// </summary>
public RedirectToUmbracoPageResult(
Guid key,
QueryString queryString,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoContextAccessor umbracoContextAccessor)
{
Key = key;
_queryString = queryString;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult"/> class.
/// </summary>
public RedirectToUmbracoPageResult(IPublishedContent? publishedContent, IPublishedUrlProvider publishedUrlProvider, IUmbracoContextAccessor umbracoContextAccessor)
{
_publishedContent = publishedContent;
Key = publishedContent?.Key ?? Guid.Empty;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult" /> class.
/// </summary>
public RedirectToUmbracoPageResult(
IPublishedContent? publishedContent,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoContextAccessor umbracoContextAccessor)
{
_publishedContent = publishedContent;
Key = publishedContent?.Key ?? Guid.Empty;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult"/> class.
/// </summary>
public RedirectToUmbracoPageResult(IPublishedContent? publishedContent, QueryString queryString, IPublishedUrlProvider publishedUrlProvider, IUmbracoContextAccessor umbracoContextAccessor)
{
_publishedContent = publishedContent;
Key = publishedContent?.Key ?? Guid.Empty;
_queryString = queryString;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoPageResult" /> class.
/// </summary>
public RedirectToUmbracoPageResult(
IPublishedContent? publishedContent,
QueryString queryString,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoContextAccessor umbracoContextAccessor)
{
_publishedContent = publishedContent;
Key = publishedContent?.Key ?? Guid.Empty;
_queryString = queryString;
_publishedUrlProvider = publishedUrlProvider;
_umbracoContextAccessor = umbracoContextAccessor;
}

public Guid Key { get; }

private string Url
private string Url
{
get
{
get
if (!string.IsNullOrWhiteSpace(_url))
{
if (!string.IsNullOrWhiteSpace(_url))
{
return _url;
}
return _url;
}

if (PublishedContent is null)
{
throw new InvalidOperationException($"Cannot redirect, no entity was found for key {Key}");
}
if (PublishedContent is null)
{
throw new InvalidOperationException($"Cannot redirect, no entity was found for key {Key}");
}

var result = _publishedUrlProvider.GetUrl(PublishedContent.Id);
var result = _publishedUrlProvider.GetUrl(PublishedContent.Id);

if (result == "#")
{
throw new InvalidOperationException(
$"Could not route to entity with key {Key}, the NiceUrlProvider could not generate a URL");
}
if (result == "#")
{
throw new InvalidOperationException(
$"Could not route to entity with key {Key}, the NiceUrlProvider could not generate a URL");
}

_url = result;
_url = result;

return _url;
}
return _url;
}
}

public Guid Key { get; }

private IPublishedContent? PublishedContent
private IPublishedContent? PublishedContent
{
get
{
get
if (!(_publishedContent is null))
{
if (!(_publishedContent is null))
{
return _publishedContent;
}

// need to get the URL for the page
_publishedContent = _umbracoContextAccessor.GetRequiredUmbracoContext().Content?.GetById(Key);

return _publishedContent;
}

// need to get the URL for the page
_publishedContent = _umbracoContextAccessor.GetRequiredUmbracoContext().Content?.GetById(Key);

return _publishedContent;
}
}

/// <inheritdoc/>
public Task ExecuteResultAsync(ActionContext context)
/// <inheritdoc />
public Task ExecuteResultAsync(ActionContext context)
{
if (context is null)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
throw new ArgumentNullException(nameof(context));
}

HttpContext httpContext = context.HttpContext;
IUrlHelperFactory urlHelperFactory = httpContext.RequestServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(context);
string destinationUrl = urlHelper.Content(Url);
HttpContext httpContext = context.HttpContext;
IUrlHelperFactory urlHelperFactory = httpContext.RequestServices.GetRequiredService<IUrlHelperFactory>();
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(context);
var destinationUrl = urlHelper.Content(Url);

if (_queryString.HasValue)
{
destinationUrl += _queryString.ToUriComponent();
}
if (_queryString.HasValue)
{
destinationUrl += _queryString.ToUriComponent();
}

httpContext.Response.Redirect(destinationUrl);
httpContext.Response.Redirect(destinationUrl);

return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
51 changes: 24 additions & 27 deletions src/Umbraco.Web.Website/ActionResults/RedirectToUmbracoUrlResult.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Umbraco.Cms.Core.Web;

namespace Umbraco.Cms.Web.Website.ActionResults
namespace Umbraco.Cms.Web.Website.ActionResults;

/// <summary>
/// Redirects to the current URL rendering an Umbraco page including it's query strings
/// </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. It is also handy if you want to persist the query strings.
/// </remarks>
public class RedirectToUmbracoUrlResult : IKeepTempDataResult
{
private readonly IUmbracoContext _umbracoContext;

/// <summary>
/// Redirects to the current URL rendering an Umbraco page including it's query strings
/// Initializes a new instance of the <see cref="RedirectToUmbracoUrlResult" /> class.
/// </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. It is also handy if you want to persist the query strings.
/// </remarks>
public class RedirectToUmbracoUrlResult : IActionResult, IKeepTempDataResult
{
private readonly IUmbracoContext _umbracoContext;

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToUmbracoUrlResult"/> class.
/// </summary>
public RedirectToUmbracoUrlResult(IUmbracoContext umbracoContext) => _umbracoContext = umbracoContext;
public RedirectToUmbracoUrlResult(IUmbracoContext umbracoContext) => _umbracoContext = umbracoContext;

/// <inheritdoc/>
public Task ExecuteResultAsync(ActionContext context)
/// <inheritdoc />
public Task ExecuteResultAsync(ActionContext context)
{
if (context is null)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
throw new ArgumentNullException(nameof(context));
}

var destinationUrl = _umbracoContext.OriginalRequestUrl.PathAndQuery;
var destinationUrl = _umbracoContext.OriginalRequestUrl.PathAndQuery;

context.HttpContext.Response.Redirect(destinationUrl);
context.HttpContext.Response.Redirect(destinationUrl);

return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
Loading