2012-10-16 60 views
0

我有一個生產環境和一個測試環境。客戶使用不同的URL設置了測試環境,如http://intranet.company.comhttp://intranettest.company.com,這很合理。測試數據庫中的內容與生產數據庫中的內容相同,我們在此存儲網頁上使用的鏈接。我沒有訪問數據庫,需要從生產的鏈接更改爲測試環境添加字符串測試到URL?

  • http://intranet.company.com應該解釋爲http://intranettest.company.com

可能有額外的結尾,例如/sites/marketing但沒有文件名(default.aspx)。鏈接也可以有一個指定的端口(在我的開發環境中,這對於整個問題並不重要,開發鏈接是http://devenv:1337/sites/marketing,這可能會解釋我的奇怪代碼。)

我做了一個代碼段,但它感覺不對,以後我可以上看到的幾個問題 - 使用它是沒有更好的方法比下面編輯我的網址

string SiteCollectionURL = SPContext.Current.Web.Site.Url.ToString(); 

char[] delimiterChar = {'/', '.'}; 
string[] splitSiteCollectionURL = SiteCollectionURL.Split(delimiterChar); 
string[] splitDepartmentLinkURL = departmentLink.Split(delimiterChar); 
string fixedUrl = departmentLink; 

if (splitSiteCollectionURL[2].Contains("test")) 
{ 
    fixedUrl = ""; 
    for (int i = 0; i < splitDepartmentLinkURL.Length; i++) 
    { 
     if (i == 0) 
     { 
      fixedUrl += splitDepartmentLinkURL[i] + "//"; 
     } 
     else if (i == 2) 
     { 
      if (splitDepartmentLinkURL[i].Contains(":")) 
      { 
       string[] splitUrlColon = splitDepartmentLinkURL[2].Split(':'); 
       fixedUrl += splitUrlColon[0] + "test:" + splitUrlColon[1] + "/"; 
      } 
      else 
      { 
       fixedUrl += splitDepartmentLinkURL[i] + "test" + "."; 
      } 
     } 
     else if (i > 2 && i < 4) 
     { 
      fixedUrl += splitDepartmentLinkURL[i] + "."; 
     } 
     else if (i >= 4 && i != splitDepartmentLinkURL.Length - 1) 
     { 
      fixedUrl += splitDepartmentLinkURL[i] + "/"; 
     } 
     else 
     { 
      fixedUrl += splitDepartmentLinkURL[i]; 
     } 
    } 
    departmentLink = fixedUrl; 
} 

回答

2

您預見的問題是什麼?如果你在你的問題中解釋他們會有幫助。但是看看在UriBuilder class

var uris = new List<String> 
{ 
    @"http://intranet.company.com", 
    @"http://myhost.company.com:1337", 
    @"http://intranet.company.com/deep/path?wat", 
    @"http://myhost.company.com:1337/some/other?path", 
}; 


foreach (var u in uris) 
{ 
    var ub = new UriBuilder(u); 
    ub.Host = "intranettest.company.com"; 
    ub.Port = 80; 

    Console.WriteLine(ub.Uri); 
} 

工作對我來說:

http://intranettest.company.com/ 
http://intranettest.company.com/ 
http://intranettest.company.com/deep/path?wat 
http://intranettest.company.com/some/other?path 
+0

Thanx!就是那個! –

0

也許我不能完全以下但着想?得到這個論點開始:
爲什麼不能做

var newUrl = oldUrl.Replace(@"http://intranet.company.com", @"http://intranettest.company.com"); 
+0

請問這種添加/網站/市場營銷,如果他們存在? –