Skip to content

Instantly share code, notes, and snippets.

@goenning
Created February 28, 2012 14:59
Show Gist options
  • Save goenning/1932971 to your computer and use it in GitHub Desktop.
Save goenning/1932971 to your computer and use it in GitHub Desktop.
draft - subdomain route with constraints
//Usage:
context.Routes.Add("Site_Home", new SubdomainRoute(
"",
"",
new { area = this.AreaName, controller = "Home", action = "Index" },
new { httpMethod = new HttpMethodConstraint("GET") },
new string[] { "SubdomainRouting.Areas.Site.Controllers" }
));
//Custom Route:
public class SubdomainRoute : Route, IRouteWithArea
{
private string[] namespaces;
public string Subdomain { get; private set; }
public SubdomainRoute(string subdomain, string url, object defaults, string[] namespaces)
: this(subdomain, url, defaults, new { }, namespaces)
{
}
public SubdomainRoute(string subdomain, string url, object defaults, object constraints, string[] namespaces)
: base(url, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints), new MvcRouteHandler())
{
this.Subdomain = subdomain;
this.namespaces = namespaces;
}
public string Area
{
get { return Convert.ToString(this.Defaults["area"]); }
}
private bool ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection)
{
if (Constraints != null)
{
foreach (var constraintsItem in Constraints)
{
if (!ProcessConstraint(httpContext, constraintsItem.Value, constraintsItem.Key, values, routeDirection))
{
return false;
}
}
}
return true;
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
string requestDomain = GetSubdomain(httpContext.Request.Headers["Host"]);
RouteData data = null;
Regex domainRegex = CreateRegex(this.Subdomain);
Regex pathRegex = CreateRegex(this.Url);
Match domainMatch = domainRegex.Match(requestDomain);
Match pathMatch = pathRegex.Match(requestPath);
if (domainMatch.Success && pathMatch.Success)
{
data = new RouteData(this, RouteHandler);
if (Defaults != null)
{
foreach (KeyValuePair<string, object> item in Defaults)
data.Values[item.Key] = item.Value;
}
if (!this.ProcessConstraints(httpContext, data.Values, RouteDirection.IncomingRequest))
return null;
for (int i = 0; i < pathMatch.Groups.Count; i++)
{
Group group = pathMatch.Groups[i];
if (group.Success)
{
string key = pathRegex.GroupNameFromNumber(i);
if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
{
if (!string.IsNullOrEmpty(group.Value))
{
data.Values[key] = group.Value;
}
}
}
}
data.DataTokens.Add("Area", data.Values["area"]);
data.DataTokens.Add("namespaces", this.namespaces);
}
return data;
}
public static string GetSubdomain(string host)
{
if (host.IndexOf(":") >= 0)
host = host.Substring(0, host.IndexOf(":"));
Regex tldRegex = new Regex(@"\.[a-z]{2,3}\.[a-z]{2}$");
host = tldRegex.Replace(host, "");
tldRegex = new Regex(@"\.[a-z]{2,4}$");
host = tldRegex.Replace(host, "");
if (host.Split('.').Length > 1)
return host.Substring(0, host.IndexOf("."));
else
return string.Empty;
}
private Regex CreateRegex(string source)
{
source = source.Replace("/", @"\/?");
source = source.Replace(".", @"\.?");
source = source.Replace("-", @"\-?");
source = source.Replace("{", @"(?<");
source = source.Replace("}", @">([a-zA-Z0-9_-]*))");
return new Regex("^" + source + "$");
}
}
@mrpaiva
Copy link

mrpaiva commented Jul 13, 2012

Eu tentei implementar esse código em VB.net da seguinte forma

context.Routes.Add("Telefonia_default", New SubdomainRoute("telefonia", "telefonia.planetavr.com.br", New With { _
.area = Me.AreaName, .controller = "LinksUteis", .action = "Index" _
}, New With { _
.httpMethod = New HttpMethodConstraint("GET") _
}, New String() {"SubdomainRouting.Areas.Site.Controllers"}))

esta correto?? pois não consegui fazer funcionar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment