2012-10-16 61 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

请问这种添加/网站/市场营销,如果他们存在? –