mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Fixed routing not matching default values
This commit is contained in:
parent
ff07909d0a
commit
1757f04787
@ -30,7 +30,7 @@ namespace Teknik.Utilities.Routing
|
|||||||
var defaults = mapping.Defaults as JObject;
|
var defaults = mapping.Defaults as JObject;
|
||||||
foreach (var defaultVal in defaults)
|
foreach (var defaultVal in defaults)
|
||||||
{
|
{
|
||||||
defaultObj.TryAdd(defaultVal.Key, defaultVal.Value);
|
defaultObj.TryAdd(defaultVal.Key, defaultVal.Value.ToObject<object>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defaultObj.TryAdd("area", mapping.Area);
|
defaultObj.TryAdd("area", mapping.Area);
|
||||||
|
@ -1,106 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Web;
|
|
||||||
using Microsoft.AspNetCore.Routing;
|
|
||||||
using Teknik.Utilities;
|
|
||||||
|
|
||||||
namespace Teknik.Utilities.Routing
|
|
||||||
{
|
|
||||||
public class SubdomainRoute : Route
|
|
||||||
{
|
|
||||||
private readonly IRouter _target;
|
|
||||||
|
|
||||||
public List<string> Subdomains { get; set; }
|
|
||||||
|
|
||||||
public List<string> Domains { get; set; }
|
|
||||||
|
|
||||||
public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeTemplate, IInlineConstraintResolver inlineConstraintResolver)
|
|
||||||
: base(router, routeTemplate, inlineConstraintResolver)
|
|
||||||
{
|
|
||||||
this.Subdomains = subdomains;
|
|
||||||
this.Domains = domains;
|
|
||||||
this._target = router;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver)
|
|
||||||
: base(router, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
|
|
||||||
{
|
|
||||||
this.Subdomains = subdomains;
|
|
||||||
this.Domains = domains;
|
|
||||||
this._target = router;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeName, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver)
|
|
||||||
: base(router, routeName, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
|
|
||||||
{
|
|
||||||
this.Subdomains = subdomains;
|
|
||||||
this.Domains = domains;
|
|
||||||
this._target = router;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override Task OnRouteMatched(RouteContext context)
|
|
||||||
{
|
|
||||||
// Only look at the subdomain if there is any route data.
|
|
||||||
if (context.RouteData != null)
|
|
||||||
{
|
|
||||||
string subdomain = context.HttpContext.Request.Query["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname.
|
|
||||||
string host = context.HttpContext.Request.Headers["Host"];
|
|
||||||
|
|
||||||
if (host != null)
|
|
||||||
{
|
|
||||||
string domain = host.GetDomain();
|
|
||||||
string curSub = host.GetSubdomain();
|
|
||||||
|
|
||||||
// special consideration for 'dev' subdomain
|
|
||||||
if (subdomain == null || subdomain == "dev")
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(curSub) && curSub == "dev")
|
|
||||||
{
|
|
||||||
// if we are on dev, and the param is dev or empty, we need to initialize it to 'www'
|
|
||||||
subdomain = "www";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (subdomain == null)
|
|
||||||
{
|
|
||||||
subdomain = curSub;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (context.RouteData.Values["sub"] == null)
|
|
||||||
{
|
|
||||||
context.RouteData.Values["sub"] = subdomain;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
subdomain = context.RouteData.Values["sub"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this route is valid for the current domain
|
|
||||||
if (context.HttpContext.Request.IsLocal() || Domains.Contains(domain))
|
|
||||||
{
|
|
||||||
// Check if this route is valid for the current subdomain ('*' means any subdomain is valid)
|
|
||||||
if (Subdomains.Contains("*") || Subdomains.Contains(subdomain))
|
|
||||||
{
|
|
||||||
context.RouteData.Values["sub"] = subdomain;
|
|
||||||
context.RouteData.Routers.Add(_target);
|
|
||||||
return _target.RouteAsync(context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override VirtualPathData GetVirtualPath(VirtualPathContext context)
|
|
||||||
{
|
|
||||||
object subdomainParam = context.HttpContext.Request.Query["sub"];
|
|
||||||
if (subdomainParam != null && context.Values["sub"] == null)
|
|
||||||
context.Values["sub"] = subdomainParam;
|
|
||||||
return base.GetVirtualPath(context); // we now have the route based on subdomain
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Routing;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Teknik.Utilities.Routing
|
|
||||||
{
|
|
||||||
public static class SubdomainRouteExtension
|
|
||||||
{
|
|
||||||
public static SubdomainRoute MapSubdomainRoute(this IRouteBuilder routeBuilder, string name, List<string> subDomains, List<string> domains, string template, object defaults)
|
|
||||||
{
|
|
||||||
return MapSubdomainRoute(routeBuilder, name, subDomains, domains, template, defaults, new { }, new { });
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SubdomainRoute MapSubdomainRoute(this IRouteBuilder routeBuilder, string name, List<string> subDomains, List<string> domains, string template, object defaults, object constraints)
|
|
||||||
{
|
|
||||||
return MapSubdomainRoute(routeBuilder, name, subDomains, domains, template, defaults, constraints, new { });
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SubdomainRoute MapSubdomainRoute(this IRouteBuilder routeBuilder, string name, List<string> subDomains, List<string> domains, string template, object defaults, object constraints, object dataTokens)
|
|
||||||
{
|
|
||||||
var inlineConstraintResolver = routeBuilder.ServiceProvider.GetRequiredService<IInlineConstraintResolver>();
|
|
||||||
|
|
||||||
SubdomainRoute route = new SubdomainRoute(
|
|
||||||
subDomains,
|
|
||||||
domains,
|
|
||||||
routeBuilder.DefaultHandler,
|
|
||||||
name,
|
|
||||||
template,
|
|
||||||
new RouteValueDictionary(defaults),
|
|
||||||
new RouteValueDictionary(constraints),
|
|
||||||
new RouteValueDictionary(dataTokens),
|
|
||||||
inlineConstraintResolver);
|
|
||||||
|
|
||||||
routeBuilder.Routes.Add(route);
|
|
||||||
|
|
||||||
return route;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,6 +9,7 @@ using System.Web;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Teknik.Utilities.Routing
|
namespace Teknik.Utilities.Routing
|
||||||
{
|
{
|
||||||
@ -57,10 +58,18 @@ namespace Teknik.Utilities.Routing
|
|||||||
|
|
||||||
// Get the endpoint mapping
|
// Get the endpoint mapping
|
||||||
var mapping = EndpointHelper.GetEndpointMapping(routeName);
|
var mapping = EndpointHelper.GetEndpointMapping(routeName);
|
||||||
if (mapping != null &&
|
if (mapping != null)
|
||||||
!routeValueDict.ContainsKey("area"))
|
|
||||||
{
|
{
|
||||||
routeValueDict.TryAdd("area", mapping.Area);
|
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<object>());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = linkGen.GetPathByAddress(url.ActionContext.HttpContext, routeName, routeValueDict);
|
var path = linkGen.GetPathByAddress(url.ActionContext.HttpContext, routeName, routeValueDict);
|
||||||
|
Loading…
Reference in New Issue
Block a user