Skip to content

Commit

Permalink
Fix deploy Umbraco as a child IIS application not working umbraco#11891
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Tkacul committed Dec 20, 2022
1 parent 3718626 commit 80f1fd7
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Umbraco.Web.BackOffice/Extensions/LinkGeneratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Umbraco.Cms.Web.BackOffice.Install;
using Umbraco.Cms.Web.Common.Routing;
using static Microsoft.AspNetCore.Routing.ControllerLinkGeneratorExtensions;
using Constants = Umbraco.Cms.Core.Constants;
namespace Umbraco.Extensions;

Expand All @@ -20,3 +21,24 @@ public static class BackofficeLinkGeneratorExtensions
ControllerExtensions.GetControllerName<InstallApiController>(),
new { area = Constants.Web.Mvc.InstallArea })?.TrimEnd(nameof(InstallApiController.GetSetup));
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static class BackofficeMIcrosoftLinkGeneratorExtensions
{
/// <summary>
/// Returns the URL for the installer
/// </summary>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetInstallerUrl(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator)
=> linkGenerator.GetPathByAction(nameof(InstallController.Index), ControllerExtensions.GetControllerName<InstallController>(), new { area = Constants.Web.Mvc.InstallArea });

/// <summary>
/// Returns the URL for the installer api
/// </summary>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetInstallerApiUrl(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator)
=> linkGenerator.GetPathByAction(
nameof(InstallApiController.GetSetup),
ControllerExtensions.GetControllerName<InstallApiController>(),
new { area = Constants.Web.Mvc.InstallArea })?.TrimEnd(nameof(InstallApiController.GetSetup));
}
194 changes: 193 additions & 1 deletion src/Umbraco.Web.Common/Extensions/LinkGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Web.Mvc;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.Routing;

namespace Umbraco.Extensions;

public static class LinkGeneratorExtensions
Expand Down Expand Up @@ -178,3 +179,194 @@ public static class LinkGeneratorExtensions
return linkGenerator.GetUmbracoApiService<T>(method.Name, methodParams);
}
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static class MicrosoftLinkGeneratorExtensions
{
/// <summary>
/// Return the back office url if the back office is installed
/// </summary>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetBackOfficeUrl(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator, IHostingEnvironment hostingEnvironment)
{
Type? backOfficeControllerType;
try
{
backOfficeControllerType = Assembly.Load("Umbraco.Web.BackOffice")
.GetType("Umbraco.Web.BackOffice.Controllers.BackOfficeController");
if (backOfficeControllerType == null)
{
return "/"; // this would indicate that the installer is installed without the back office
}
}
catch
{
return
hostingEnvironment
.ApplicationVirtualPath; // this would indicate that the installer is installed without the back office
}

LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetPathByAction(
"Default",
ControllerExtensions.GetControllerName(backOfficeControllerType),
new { area = Constants.Web.Mvc.BackOfficeApiArea });
}

/// <summary>
/// Return the Url for a Web Api service
/// </summary>
/// <typeparam name="T">The <see cref="UmbracoApiControllerBase" /></typeparam>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoApiService<T>(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator, string actionName, object? id = null)
where T : UmbracoApiControllerBase
{
LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetUmbracoControllerUrl(actionName, typeof(T), new Dictionary<string, object?>
{
["id"] = id
});
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoApiService<T>(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator,
string actionName, IDictionary<string, object?>? values)
where T : UmbracoApiControllerBase
{
LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetUmbracoControllerUrl(actionName, typeof(T), values);
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoApiServiceBaseUrl<T>(
this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator,
Expression<Func<T, object?>> methodSelector)
where T : UmbracoApiControllerBase
{
LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector);
if (method == null)
{
throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) +
" or the result ");
}

return umbracoLinkGenerator.GetUmbracoApiService<T>(method.Name)?.TrimEnd(method.Name);
}

/// <summary>
/// Return the Url for an Umbraco controller
/// </summary>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoControllerUrl(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator, string actionName, string controllerName, string? area, IDictionary<string, object?>? dict = null)
{
if (actionName == null)
{
throw new ArgumentNullException(nameof(actionName));
}

if (string.IsNullOrWhiteSpace(actionName))
{
throw new ArgumentException(
"Value can't be empty or consist only of white-space characters.",
nameof(actionName));
}

if (controllerName == null)
{
throw new ArgumentNullException(nameof(controllerName));
}

if (string.IsNullOrWhiteSpace(controllerName))
{
throw new ArgumentException(
"Value can't be empty or consist only of white-space characters.",
nameof(controllerName));
}

if (dict is null)
{
dict = new Dictionary<string, object?>();
}

if (!area.IsNullOrWhiteSpace())
{
dict["area"] = area!;
}

IDictionary<string, object?> values = dict.Aggregate(
new ExpandoObject() as IDictionary<string, object?>,
(a, p) =>
{
a.Add(p.Key, p.Value);
return a;
});

LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetPathByAction(actionName, controllerName, values);
}

/// <summary>
/// Return the Url for an Umbraco controller
/// </summary>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoControllerUrl(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator, string actionName, Type controllerType, IDictionary<string, object?>? values = null)
{
if (actionName == null)
{
throw new ArgumentNullException(nameof(actionName));
}

if (string.IsNullOrWhiteSpace(actionName))
{
throw new ArgumentException(
"Value can't be empty or consist only of white-space characters.",
nameof(actionName));
}

if (controllerType == null)
{
throw new ArgumentNullException(nameof(controllerType));
}

var area = string.Empty;

if (!typeof(ControllerBase).IsAssignableFrom(controllerType))
{
throw new InvalidOperationException($"The controller {controllerType} is of type {typeof(ControllerBase)}");
}

PluginControllerMetadata metaData = PluginController.GetMetadata(controllerType);
if (metaData.AreaName.IsNullOrWhiteSpace() == false)
{
// set the area to the plugin area
area = metaData.AreaName;
}

LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetUmbracoControllerUrl(actionName, ControllerExtensions.GetControllerName(controllerType), area, values);
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoApiService<T>(
this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator,
Expression<Func<T, object>> methodSelector)
where T : UmbracoApiController
{
MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector);
IDictionary<string, object?>? methodParams = ExpressionHelper.GetMethodParams(methodSelector);
if (method == null)
{
throw new MissingMethodException(
$"Could not find the method {methodSelector} on type {typeof(T)} or the result ");
}

if (methodParams?.Any() == false)
{
return linkGenerator.GetUmbracoApiService<T>(method.Name);
}

LinkGenerator umbracoLinkGenerator = StaticServiceProvider.Instance.GetRequiredService<LinkGenerator>();
return umbracoLinkGenerator.GetUmbracoApiService<T>(method.Name, methodParams);
}
}
42 changes: 42 additions & 0 deletions src/Umbraco.Web.Website/Extensions/LinkGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,45 @@ public static class LinkGeneratorExtensions
typeof(T),
new Dictionary<string, object?> { ["id"] = id });
}

[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static class MicrosoftLinkGeneratorExtensions
{
/// <summary>
/// Return the Url for a Surface Controller
/// </summary>
/// <typeparam name="T">The <see cref="SurfaceController" /></typeparam>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoSurfaceUrl<T>(
this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator,
Expression<Func<T, object>> methodSelector)
where T : SurfaceController
{
MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector);
IDictionary<string, object?>? methodParams = ExpressionHelper.GetMethodParams(methodSelector);

if (method == null)
{
throw new MissingMethodException(
$"Could not find the method {methodSelector} on type {typeof(T)} or the result ");
}

if (methodParams is null || methodParams.Any() == false)
{
return linkGenerator.GetUmbracoSurfaceUrl<T>(method.Name);
}

return linkGenerator.GetUmbracoSurfaceUrl<T>(method.Name, methodParams);
}

/// <summary>
/// Return the Url for a Surface Controller
/// </summary>
/// <typeparam name="T">The <see cref="SurfaceController" /></typeparam>
[Obsolete("Please use Umbraco.Cms.Web.Common.Routing.LinkGenerator instead.")]
public static string? GetUmbracoSurfaceUrl<T>(this Microsoft.AspNetCore.Routing.LinkGenerator linkGenerator, string actionName, object? id = null)
where T : SurfaceController => linkGenerator.GetUmbracoControllerUrl(
actionName,
typeof(T),
new Dictionary<string, object?> { ["id"] = id });
}

0 comments on commit 80f1fd7

Please sign in to comment.