2019-01-25 08:47:19 +01:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
2018-06-15 02:57:03 +02:00
using Microsoft.AspNetCore.Mvc.Routing ;
using System ;
2015-12-22 08:54:22 +01:00
using System.Linq ;
2015-12-23 21:14:34 +01:00
using System.Text.RegularExpressions ;
2015-12-22 08:54:22 +01:00
using System.Web ;
2017-01-18 09:12:22 +01:00
namespace Teknik.Utilities
2015-12-22 08:54:22 +01:00
{
public static class UrlExtensions
{
2018-06-15 02:57:03 +02:00
public static string SubRouteUrl ( this IUrlHelper url , string sub , string routeName )
2015-12-23 21:14:34 +01:00
{
2017-01-18 09:12:22 +01:00
return url . SubRouteUrl ( sub , routeName , null , string . Empty ) ;
2016-10-08 07:21:28 +02:00
}
2018-06-15 02:57:03 +02:00
public static string SubRouteUrl ( this IUrlHelper url , string sub , string routeName , string hostOverride )
2016-10-08 07:21:28 +02:00
{
2017-01-18 09:12:22 +01:00
return url . SubRouteUrl ( sub , routeName , null , hostOverride ) ;
2016-10-08 07:21:28 +02:00
}
2018-06-15 02:57:03 +02:00
public static string SubRouteUrl ( this IUrlHelper url , string sub , string routeName , object routeValues )
2016-10-08 07:21:28 +02:00
{
2017-01-18 09:12:22 +01:00
return url . SubRouteUrl ( sub , routeName , routeValues , string . Empty ) ;
2015-12-23 21:14:34 +01:00
}
2015-12-22 08:54:22 +01:00
/// <summary>
2015-12-23 21:14:34 +01:00
/// Generates a full URL given the specified sub domain and route name
2015-12-22 08:54:22 +01:00
/// </summary>
/// <param name="url"></param>
/// <param name="sub"></param>
2015-12-23 21:14:34 +01:00
/// <param name="routeName"></param>
2015-12-22 08:54:22 +01:00
/// <param name="routeValues"></param>
/// <returns></returns>
2018-06-15 02:57:03 +02:00
public static string SubRouteUrl ( this IUrlHelper url , string sub , string routeName , object routeValues , string hostOverride )
2015-12-22 08:54:22 +01:00
{
2018-06-15 02:57:03 +02:00
string host = url . ActionContext . HttpContext . Request . Host . Value ;
2015-12-23 08:29:02 +01:00
2016-02-06 01:48:00 +01:00
string domain = host . GetDomain ( ) ;
2015-12-22 08:54:22 +01:00
string rightUrl = string . Empty ;
// get current subdomain
2015-12-23 21:14:34 +01:00
string curSub = host . GetSubdomain ( ) ;
2015-12-22 08:54:22 +01:00
// Grab the sub from parameters if it exists
2018-06-15 02:57:03 +02:00
string subParam = url . ActionContext . HttpContext . Request . Query [ "sub" ] ; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
2015-12-23 08:29:02 +01:00
2015-12-22 08:54:22 +01:00
// If the param is not being used, we will use the curSub
if ( string . IsNullOrEmpty ( subParam ) )
{
2016-01-22 20:38:28 +01:00
// If we are on dev and no subparam, we need to set the subparam to the specified sub
subParam = ( curSub = = "dev" ) ? sub : string . Empty ;
2015-12-23 08:29:02 +01:00
string firstSub = ( curSub = = "dev" ) ? "dev" : sub ;
2015-12-23 21:14:34 +01:00
if ( ! string . IsNullOrEmpty ( firstSub ) )
{
domain = firstSub + "." + domain ;
}
2015-12-22 08:54:22 +01:00
}
else
{
2016-01-22 20:38:28 +01:00
string firstSub = ( curSub = = "dev" ) ? "dev" : curSub ;
if ( ! string . IsNullOrEmpty ( firstSub ) )
{
domain = firstSub + "." + domain ;
}
else
{
domain = host ;
}
2015-12-23 08:29:02 +01:00
}
2015-12-22 08:54:22 +01:00
2015-12-23 21:14:34 +01:00
try
{
2018-06-15 02:57:03 +02:00
rightUrl = url . RouteUrl ( new UrlRouteContext ( ) { RouteName = routeName , Values = routeValues } ) ;
2015-12-23 21:14:34 +01:00
}
2018-10-26 07:22:53 +02:00
catch ( ArgumentException ex )
2015-12-23 21:14:34 +01:00
{
}
2018-06-15 02:57:03 +02:00
string fullHost = string . Format ( "{0}://{1}" , url . ActionContext . HttpContext . Request . Scheme , domain ) ;
2017-01-18 09:12:22 +01:00
if ( ! string . IsNullOrEmpty ( hostOverride ) )
2016-10-08 07:21:28 +02:00
{
2017-01-18 09:12:22 +01:00
fullHost = hostOverride . TrimEnd ( '/' ) ;
2016-10-08 07:21:28 +02:00
}
string absoluteAction = string . Format ( "{0}{1}" , fullHost , rightUrl ) ;
2015-12-23 08:29:02 +01:00
2016-01-22 20:38:28 +01:00
if ( ! string . IsNullOrEmpty ( subParam ) )
2015-12-23 08:29:02 +01:00
{
absoluteAction = absoluteAction . SetUrlParameter ( "sub" , sub ) ;
}
2015-12-22 08:54:22 +01:00
return absoluteAction ;
}
2016-10-08 07:21:28 +02:00
2015-12-23 08:29:02 +01:00
public static string GetUrlParameters ( this string url )
{
Uri uri = new Uri ( url ) ;
var queryParts = HttpUtility . ParseQueryString ( uri . Query ) ;
return queryParts . ToString ( ) ;
}
public static string SetUrlParameter ( this string url , string paramName , string value )
{
return new Uri ( url ) . SetParameter ( paramName , value ) . ToString ( ) ;
}
public static Uri SetParameter ( this Uri url , string paramName , string value )
{
var queryParts = HttpUtility . ParseQueryString ( url . Query ) ;
queryParts [ paramName ] = value ;
return new Uri ( url . AbsoluteUriExcludingQuery ( ) + '?' + queryParts . ToString ( ) ) ;
}
public static string AbsoluteUriExcludingQuery ( this Uri url )
{
return url . AbsoluteUri . Split ( '?' ) . FirstOrDefault ( ) ? ? String . Empty ;
}
2017-01-18 09:12:22 +01:00
2015-12-23 21:14:34 +01:00
public static string GetSubdomain ( this string host )
{
if ( host . IndexOf ( ":" ) > = 0 )
host = host . Substring ( 0 , host . IndexOf ( ":" ) ) ;
Regex tldRegex = new Regex ( @"\.[a-z]{2,3}\.[a-z]{2}$" ) ;
host = tldRegex . Replace ( host , "" ) ;
tldRegex = new Regex ( @"\.[a-z]{2,4}$" ) ;
host = tldRegex . Replace ( host , "" ) ;
if ( host . Split ( '.' ) . Length > 1 )
return host . Substring ( 0 , host . IndexOf ( "." ) ) ;
else
return string . Empty ;
}
2016-01-26 08:24:17 +01:00
2018-06-15 02:57:03 +02:00
public static string GetSubdomain ( this IUrlHelper url )
2016-01-26 08:24:17 +01:00
{
2018-06-15 02:57:03 +02:00
string host = url . ActionContext . HttpContext . Request . Host . Value ;
2016-01-26 08:24:17 +01:00
// Grab the sub from parameters if it exists
2018-06-15 02:57:03 +02:00
string subParam = url . ActionContext . HttpContext . Request . Query [ "sub" ] ; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
2016-01-26 08:24:17 +01:00
if ( string . IsNullOrEmpty ( subParam ) )
{
// If we are on dev and no subparam, we need to set the subparam to the specified sub
subParam = host . GetSubdomain ( ) ;
}
return subParam ;
}
2016-02-06 01:48:00 +01:00
public static string GetDomain ( this string host )
{
string domain = host ;
var split = host . Split ( '.' ) ; // split the host by '.'
if ( split . Count ( ) > 2 )
{
int index = host . IndexOf ( '.' ) + 1 ;
if ( index > = 0 & & index < host . Length )
domain = host . Substring ( index , ( host . Length - index ) ) ;
}
return domain ;
}
public static string GetDomain ( this Uri uri )
{
string domain = uri . Host ;
var split = uri . Host . Split ( '.' ) ; // split the host by '.'
if ( split . Count ( ) > 2 )
{
int index = uri . Host . IndexOf ( '.' ) + 1 ;
if ( index > = 0 & & index < uri . Host . Length )
domain = uri . Host . Substring ( index , ( uri . Host . Length - index ) ) ;
}
return domain ;
}
2018-06-15 02:57:03 +02:00
public static string GetActive ( this IUrlHelper url , params string [ ] subs )
2016-01-26 08:24:17 +01:00
{
2019-01-25 08:47:19 +01:00
return url . GetActive ( null , null , subs ) ;
}
public static string GetActive ( this IUrlHelper url , string controller )
{
return url . GetActive ( controller , null ) ;
}
public static string GetActive ( this IUrlHelper url , string controller , string action , params string [ ] subs )
{
var curSub = url . GetSubdomain ( ) ;
var curController = url . ActionContext . RouteData . Values [ "Controller" ] ? . ToString ( ) ;
var curAction = url . ActionContext . RouteData . Values [ "Action" ] ? . ToString ( ) ;
2016-01-26 08:24:17 +01:00
foreach ( string sub in subs )
{
if ( curSub = = sub )
{
2019-01-25 08:47:19 +01:00
if ( ( string . IsNullOrEmpty ( controller ) | | curController = = controller ) & &
( string . IsNullOrEmpty ( action ) | | curAction = = action ) )
return "active" ;
2016-01-26 08:24:17 +01:00
}
}
2019-01-25 08:47:19 +01:00
if ( ! subs . Any ( ) & &
( string . IsNullOrEmpty ( controller ) | | curController = = controller ) & &
( string . IsNullOrEmpty ( action ) | | curAction = = action ) )
{
return "active" ;
}
2016-01-26 08:24:17 +01:00
return string . Empty ;
}
2016-02-24 20:58:56 +01:00
public static bool IsValidUrl ( this string url )
{
Uri uriResult ;
2016-02-24 22:08:34 +01:00
bool result = Uri . TryCreate ( url , UriKind . Absolute , out uriResult ) ;
2016-02-24 20:58:56 +01:00
if ( result )
{
result = uriResult . IsWellFormedOriginalString ( ) ;
}
return result ;
}
2018-06-15 02:57:03 +02:00
public static string FullURL ( this IUrlHelper helper , string virtualPath )
{
var url = string . Format ( "{0}://{1}{2}" , helper . ActionContext . HttpContext . Request . Scheme , helper . ActionContext . HttpContext . Request . Host . ToUriComponent ( ) , helper . Content ( virtualPath ) ) ;
return url ;
}
2015-12-22 08:54:22 +01:00
}
}