1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added more subdomain routing for generating urls.

Fixed Home to have proper routing
This commit is contained in:
Uncled1023 2015-12-21 15:59:08 -08:00
parent 69d1f46234
commit 7367d889e9
2 changed files with 38 additions and 8 deletions

View File

@ -41,9 +41,16 @@ namespace Teknik
if (subdomain == null)
{
string host = httpContext.Request.Headers["Host"];
int index = host.IndexOf('.');
if (index >= 0)
subdomain = host.Substring(0, index);
if (host.Split('.').Count() > 2)
{
int index = host.IndexOf('.');
if (index >= 0)
subdomain = host.Substring(0, index);
}
else
{
subdomain = string.Empty;
}
}
routeData.Values["sub"] = subdomain;
@ -59,7 +66,30 @@ namespace Teknik
object subdomainParam = requestContext.HttpContext.Request.Params["sub"];
if (subdomainParam != null)
values["sub"] = subdomainParam;
return base.GetVirtualPath(requestContext, values);
var data = base.GetVirtualPath(requestContext, values); // we now have the route based on subdomain
if (data != null)
{
// we should generate the URL now
var split = requestContext.HttpContext.Request.Url.Host.Split('.'); // split the host by '.'
if (split.Count() > 0 && !split[0].ToLower().Contains("dev")) // fire only if the hostname doesn't contain the 'dev' subdomain
{
// Get the current domain
string domain = requestContext.HttpContext.Request.Url.Host;
if (split.Count() > 2)
{
domain = split[1] + "." + split[2];
}
// now let's replace the subdomain
if (data.VirtualPath.StartsWith("/"))
data.VirtualPath = data.VirtualPath.Substring(1);
// generate the full URL, not just relevent path
data.VirtualPath = string.Format("{0}.{1}/{2}", subdomainParam, domain, data.VirtualPath);
}
}
return data;
}
}
}

View File

@ -19,21 +19,21 @@ namespace Teknik.Areas.Home
context.MapSubdomainRoute(
"Home_dev", // Route name
"dev",
"Home/{controller}/{action}", // URL with parameters
"Home", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.HomeController).Namespace }
);
context.MapSubdomainRoute(
"Home_subdomain", // Route name
"www",
"{controller}/{action}", // URL with parameters
"", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.HomeController).Namespace }
);
context.MapSubdomainRoute(
"Home_default", // Route name
null,
"{controller}/{action}", // URL with parameters
string.Empty,
"", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.HomeController).Namespace }
);