using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json.Linq;
namespace Teknik.Utilities.Routing
{
public static class UrlExtensions
{
public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName)
{
return url.SubRouteUrl(sub, routeName, null, string.Empty);
}
public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, string hostOverride)
{
return url.SubRouteUrl(sub, routeName, null, hostOverride);
}
public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, object routeValues)
{
return url.SubRouteUrl(sub, routeName, routeValues, string.Empty);
}
///
/// Generates a full URL given the specified sub domain and route name
///
///
///
///
///
///
public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, object routeValues, string hostOverride)
{
var linkGen = url.ActionContext.HttpContext.RequestServices.GetService();
var env = url.ActionContext.HttpContext.RequestServices.GetService();
string host = url.ActionContext.HttpContext.Request.Host.Value;
string domain = host;
// Generate a new domain if we aren't in development
if (!env.IsEnvironment(Environments.Development) &&
!string.IsNullOrEmpty(sub))
{
domain = sub + "." + domain.GetDomain();
}
string fullHost = string.Format("{0}://{1}", url.ActionContext.HttpContext.Request.Scheme, domain);
var routeValueDict = new RouteValueDictionary(routeValues);
// Get the endpoint mapping
var mapping = EndpointHelper.GetEndpointMapping(routeName);
if (mapping != null)
{
routeValueDict.TryAdd("area", mapping.Area);
if (mapping.Defaults != null)
{
var defaults = mapping.Defaults as JObject;
foreach (var defaultVal in defaults)
{
routeValueDict.TryAdd(defaultVal.Key, defaultVal.Value.ToObject