Browse Source

Added more subdomain routing for generating urls.

Fixed Home to have proper routing
tags/2.0.3
Teknikode 5 years ago
parent
commit
7367d889e9
2 changed files with 38 additions and 8 deletions
  1. 34
    4
      Teknik/App_Start/SubdomainRoute.cs
  2. 4
    4
      Teknik/Areas/Home/HomeAreaRegistration.cs

+ 34
- 4
Teknik/App_Start/SubdomainRoute.cs 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;
}
}
}

+ 4
- 4
Teknik/Areas/Home/HomeAreaRegistration.cs 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 }
);

Loading…
Cancel
Save