C# URL Handling to allow either separate sites or virtual directories? -
i've got question more own learning experience i've found solution, or 2 more specific.
the issue have web service connect application, web service can deployed different ways, ie, iis virtual directories, or separate sites. obviously, these have impact on url. url in case gets pulled configuration file wherever application installed, application has able handle whatever gets pulled there.
i wrote quick chunk in sample console example. first method doesn't work in case of virtual directories issue arises. other 2 methods return correct result, i'm curious, if there reason why 1 wouldn't replace string on using lastindexof, , if maybe there's solution?
uri sitenameurl = new uri("http://sitename.test.com/packagedlservice.aspx"); console.writeline(string.format("url scenario 1: {0}",sitenameurl)); string path = sitenameurl.getleftpart(uripartial.authority); string uristr = string.format(@"{0}/sitename/packages/{1}/currentpackage.zip", path, "1.0"); console.writeline(string.format("url method 1 (broken): {0}", uristr)); path = sitenameurl.tostring().replace("/packagedlservice.aspx",""); uristr = string.format(@"{0}/packages/{1}/currentpackage.zip", path, "1.0"); console.writeline(string.format("url method 2: {0}", uristr)); path = sitenameurl.tostring().substring(0, sitenameurl.tostring().lastindexofany(new char[] { '/', '\\' }) + 1); uristr = string.format(@"{0}packages/{1}/currentpackage.zip", path, "1.0"); console.writeline(string.format("url method 3: {0}", uristr)); console.writeline(); // add line break between examples uri sitenameurl1 = new uri("http://www.test.com/sitename/packagedlservice.aspx"); console.writeline(string.format("url scenario 2: {0}", sitenameurl1)); string path1 = sitenameurl1.getleftpart(uripartial.authority); string uristr1 = string.format(@"{0}/sitename/packages/{1}/currentpackage.zip", path1, "1.0"); console.writeline(string.format("url method 1 (broken): {0}", uristr1)); path1 = sitenameurl1.tostring().replace("/packagedlservice.aspx",""); uristr1 = string.format(@"{0}/packages/{1}/currentpackage.zip", path1, "1.0"); console.writeline(string.format("url method 2: {0}", uristr1)); path1 = sitenameurl1.tostring().substring(0, sitenameurl1.tostring().lastindexofany(new char[] { '/', '\\' }) + 1); uristr1 = string.format(@"{0}packages/{1}/currentpackage.zip", path1, "1.0"); console.writeline(string.format("url method 3: {0}", uristr1));
Comments
Post a Comment