Browse Source

Added more subdomain routing for generating urls.

Fixed Home to have proper routing
master
Teknikode 7 years ago
parent
commit
7367d889e9
  1. 38
      Teknik/App_Start/SubdomainRoute.cs
  2. 8
      Teknik/Areas/Home/HomeAreaRegistration.cs

38
Teknik/App_Start/SubdomainRoute.cs

@ -41,9 +41,16 @@ namespace Teknik @@ -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 @@ -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;
}
}
}

8
Teknik/Areas/Home/HomeAreaRegistration.cs

@ -19,21 +19,21 @@ namespace Teknik.Areas.Home @@ -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 }
);

Loading…
Cancel
Save