Tekerleği yeniden icat ettiğimi hissediyordum, bu yüzden kalabalığa buraya sorarım diye düşündüm. Böyle bir kod parçası olduğunu düşünün:C# içinde parçaları kullanarak güvenilir bir şekilde nasıl URL oluşturabilirim?
string protocol = "http"; // Pretend this value is retrieved from a config file
string host = "www.google.com"; // Pretend this value is retrieved from a config file
string path = "plans/worlddomination.html"; // Pretend this value is retrieved from a config file
Ben url "http://www.google.com/plans/worlddomination.html" inşa etmek istiyorum.
protocol = protocol.EndsWith("://") ? protocol : protocol + "://";
path = path.StartsWith("/") ? path : "/" + path;
string fullUrl = string.Format("{0}{1}{2}", protocol, host, path);
Ne istiyorum gerçekten böyle API çeşit: Böyle sevimsiz bir kod yazarak bu yapmaya devam
UrlBuilder builder = new UrlBuilder();
builder.Protocol = protocol;
builder.Host = host;
builder.Path = path;
builder.QueryString = null;
string fullUrl = builder.ToString();
bu yerde .NET framework var inanmalısınız, ama hiçbir yerde Karşılaştım.
Dayanıklılık (yani, hiç hatalı biçimlendirilmemiş) URL'ler oluşturmanın en iyi yolu nedir? UriBuilder class
Oku Alex Black'in cevap, daha sonra Tıklayın: http: //social.msdn .microsoft.com/Arama/tr /? Ayrıntılandırma =% 27 2c117 & Sorgu = Uri + Builder –