2020-05-13 14:49:00 +10:00
using System ;
2020-06-22 14:11:01 +02:00
using System.Collections.Generic ;
using System.Dynamic ;
2020-06-09 07:49:26 +02:00
using System.Linq ;
2020-05-13 14:49:00 +10:00
using Umbraco.Core ;
using Microsoft.AspNetCore.Routing ;
using System.Reflection ;
2020-05-13 17:14:09 +10:00
using Umbraco.Web.Common.Install ;
2020-05-18 15:16:09 +10:00
using Umbraco.Core.Hosting ;
2020-05-25 23:15:32 +10:00
using System.Linq.Expressions ;
using Umbraco.Web.Common.Controllers ;
2020-06-03 17:47:32 +10:00
using System.Linq ;
2020-05-13 14:49:00 +10:00
namespace Umbraco.Extensions
{
public static class LinkGeneratorExtensions
{
/// <summary>
/// Return the back office url if the back office is installed
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
2020-05-18 15:16:09 +10:00
public static string GetBackOfficeUrl ( this LinkGenerator linkGenerator , IHostingEnvironment hostingEnvironment )
2020-05-13 14:49:00 +10:00
{
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
}
2020-05-18 15:15:53 +10:00
catch
2020-05-13 14:49:00 +10:00
{
2020-05-18 15:16:09 +10:00
return hostingEnvironment . ApplicationVirtualPath ; // this would indicate that the installer is installed without the back office
2020-05-13 14:49:00 +10:00
}
2020-05-13 17:14:09 +10:00
2020-05-19 14:02:12 +02:00
return linkGenerator . GetPathByAction ( "Default" , ControllerExtensions . GetControllerName ( backOfficeControllerType ) , values : new { area = Constants . Web . Mvc . BackOfficeApiArea } ) ;
2020-05-13 14:49:00 +10:00
}
2020-05-13 17:14:09 +10:00
/// <summary>
/// Returns the URL for the installer
/// </summary>
/// <param name="linkGenerator"></param>
/// <returns></returns>
public static string GetInstallerUrl ( this LinkGenerator linkGenerator )
{
2020-05-18 15:12:30 +10:00
return linkGenerator . GetPathByAction ( nameof ( InstallController . Index ) , ControllerExtensions . GetControllerName < InstallController > ( ) , new { area = Constants . Web . Mvc . InstallArea } ) ;
2020-05-13 17:14:09 +10:00
}
2020-05-25 23:15:32 +10:00
2020-08-21 13:53:27 +02:00
/// <summary>
/// Returns the URL for the installer api
/// </summary>
/// <param name="linkGenerator"></param>
/// <returns></returns>
public static string GetInstallerApiUrl ( this LinkGenerator linkGenerator )
{
return linkGenerator . GetPathByAction ( nameof ( InstallController . Index ) , ControllerExtensions . GetControllerName < InstallApiController > ( ) , new { area = Constants . Web . Mvc . InstallArea } ) ;
}
2020-05-25 23:15:32 +10:00
/// <summary>
/// Return the Url for a Web Api service
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="id"></param>
/// <returns></returns>
public static string GetUmbracoApiService < T > ( this LinkGenerator linkGenerator , string actionName , object id = null )
where T : UmbracoApiControllerBase
{
2020-06-22 14:11:01 +02:00
return linkGenerator . GetUmbracoApiService ( actionName , typeof ( T ) , new Dictionary < string , object > ( )
{
["id"] = id
} ) ;
2020-05-25 23:15:32 +10:00
}
2020-06-22 14:11:01 +02:00
public static string GetUmbracoApiService < T > ( this LinkGenerator linkGenerator , string actionName , IDictionary < string , object > values )
where T : UmbracoApiControllerBase
{
return linkGenerator . GetUmbracoApiService ( actionName , typeof ( T ) , values ) ;
}
2020-06-03 17:47:32 +10:00
2020-05-25 23:15:32 +10:00
public static string GetUmbracoApiServiceBaseUrl < T > ( this LinkGenerator linkGenerator , Expression < Func < T , object > > methodSelector )
where T : UmbracoApiControllerBase
{
var method = ExpressionHelper . GetMethodInfo ( methodSelector ) ;
if ( method = = null )
{
throw new MissingMethodException ( "Could not find the method " + methodSelector + " on type " + typeof ( T ) + " or the result " ) ;
}
return linkGenerator . GetUmbracoApiService < T > ( method . Name ) . TrimEnd ( method . Name ) ;
}
/// <summary>
/// Return the Url for a Web Api service
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <param name="area"></param>
/// <param name="id"></param>
/// <returns></returns>
2020-06-22 14:11:01 +02:00
public static string GetUmbracoApiService ( this LinkGenerator linkGenerator , string actionName , string controllerName , string area , IDictionary < string , object > dict = null )
2020-05-25 23:15:32 +10:00
{
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 ) ) ;
2020-06-22 14:11:01 +02:00
if ( dict is null )
2020-05-25 23:15:32 +10:00
{
2020-06-22 14:11:01 +02:00
dict = new Dictionary < string , object > ( ) ;
2020-05-25 23:15:32 +10:00
}
2020-06-22 14:11:01 +02:00
if ( ! area . IsNullOrWhiteSpace ( ) )
2020-05-25 23:15:32 +10:00
{
2020-06-22 14:11:01 +02:00
dict [ "area" ] = area ;
2020-05-25 23:15:32 +10:00
}
2020-06-22 14:11:01 +02:00
var values = dict . Aggregate ( new ExpandoObject ( ) as IDictionary < string , object > ,
( a , p ) = > { a . Add ( p . Key , p . Value ) ; return a ; } ) ;
return linkGenerator . GetPathByAction ( actionName , controllerName , values ) ;
2020-05-25 23:15:32 +10:00
}
/// <summary>
/// Return the Url for a Web Api service
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="apiControllerType"></param>
/// <param name="id"></param>
/// <returns></returns>
2020-06-22 14:11:01 +02:00
public static string GetUmbracoApiService ( this LinkGenerator linkGenerator , string actionName , Type apiControllerType , IDictionary < string , object > values = null )
2020-05-25 23:15:32 +10:00
{
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 ( apiControllerType = = null ) throw new ArgumentNullException ( nameof ( apiControllerType ) ) ;
var area = "" ;
if ( ! typeof ( UmbracoApiControllerBase ) . IsAssignableFrom ( apiControllerType ) )
throw new InvalidOperationException ( $"The controller {apiControllerType} is of type {typeof(UmbracoApiControllerBase)}" ) ;
var metaData = PluginController . GetMetadata ( apiControllerType ) ;
if ( metaData . AreaName . IsNullOrWhiteSpace ( ) = = false )
{
//set the area to the plugin area
area = metaData . AreaName ;
}
2020-06-22 14:11:01 +02:00
return linkGenerator . GetUmbracoApiService ( actionName , ControllerExtensions . GetControllerName ( apiControllerType ) , area , values ) ;
2020-05-25 23:15:32 +10:00
}
2020-06-09 07:49:26 +02:00
public static string GetUmbracoApiService < T > ( this LinkGenerator linkGenerator , Expression < Func < T , object > > methodSelector )
where T : UmbracoApiController
{
var method = ExpressionHelper . GetMethodInfo ( methodSelector ) ;
var methodParams = ExpressionHelper . GetMethodParams ( methodSelector ) ;
if ( method = = null )
{
2020-06-11 13:04:01 +02:00
throw new MissingMethodException (
$"Could not find the method {methodSelector} on type {typeof(T)} or the result " ) ;
2020-06-09 07:49:26 +02:00
}
if ( methodParams . Any ( ) = = false )
{
return linkGenerator . GetUmbracoApiService < T > ( method . Name ) ;
}
2020-06-22 14:11:01 +02:00
return linkGenerator . GetUmbracoApiService < T > ( method . Name , methodParams ) ;
2020-06-09 07:49:26 +02:00
}
2020-05-13 14:49:00 +10:00
}
}