且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用URL重写创建类似路径的动态子域

更新时间:2023-11-27 08:36:58

protected void Application_BeginRequest(Object sender, EventArgs e)
{   
    var SubDomain = GetSubDomain(HttpContext.Current.Request.Host);
    // this is a simple example, you can place your variables base on subdomain
    if(!String.IsNullOrEmpty(SubDomain))
       RewritePath(HttpContext.Current.Request.Path + SubDomain + "/", false);  
}

// from : http://madskristensen.net/post/Retrieve-the-subdomain-from-a-URL-in-C.aspx
private static string GetSubDomain(Uri url)
{
  string host = url.Host;
  if (host.Split('.').Length > 1)
  {
    int index = host.IndexOf(".");
    return host.Substring(0, index);
  }

  return null;
}