2011-07-02 46 views
1

我試圖做的是下面,我想改寫這個網址類型:Asp.net 4 URL重寫一個子域作爲文件夾

blog.domain.com/... 

domain.com/blog/... 

它在共享主機環境中,使用IIS7/ASP.Net 4.另一件事是域和博客子域有不同的aps運行。我一直在尋找幾個小時來尋找最好的解決方案,我希望有人能夠在這裏指導我一點。謝謝!

回答

0

這是第一個想法的嘗試。

// keep a valid list somewhere 
    List<string> cValidNames = new List<string>(); 
    cValidNames.Add("blog"); 

    // get the host 
    //string TheHost = Request.Url.Host; 
    string TheHost = "blog.domain.com"; 

    // find the first part, assume that the domain is standard and not change 
    int WhereStarts = TheHost.IndexOf(".domain.com"); 

    // if we found it 
    if(WhereStarts != -1) 
    { 
     string cTheFirstPart = TheHost.Substring(0, WhereStarts); 

     // if its on the valid domain (exclude the www) 
     if (cValidNames.Contains(cTheFirstPart)) 
     { 
      // now I add in the frond the name and continue with the rest url 
      string cFinalPath = "/" + cTheFirstPart + Request.RawUrl; 

      // now rewrite it. 
      HttpContext.Current.RewritePath(cFinalPath, false); 

      return; 
     } 
    }