Reputation: 10681
I have a production environment and a test environment. The customer has set up the test environment with different URL's like http://intranet.company.com
and http://intranettest.company.com
, which make perfect sense. The content in the test database is the same as in the production database, where we store a link that is used on the web page. I don't have access to the database and need to change the link from production to test environment
http://intranet.company.com
should be parsed to http://intranettest.company.com
There could be additional endings, such as /sites/marketing
but without a file name (default.aspx
). The link can also have a port specified (in my development environment, which is not really important for the question at large. The development link is http://devenv:1337/sites/marketing
which might explain my strange code.
I've made a snippet, but it doesn't feel right, and I can see several problems later on - using it. Is there a better way than the following to edit my URL?
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;
}
Upvotes: 0
Views: 105
Reputation: 151644
What are the problems you foresee? It would help if you explain them in your question. But take a look at the 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);
}
Works for me:
http://intranettest.company.com/
http://intranettest.company.com/
http://intranettest.company.com/deep/path?wat
http://intranettest.company.com/some/other?path
Upvotes: 2
Reputation: 3315
Maybe i'm not completely following but for the sake of getting this argument started :
Why not do
var newUrl = oldUrl.Replace(@"http://intranet.company.com", @"http://intranettest.company.com");
Upvotes: 0