Created
May 8, 2019 20:51
-
-
Save brunossn/1e81d6c53d849bddc16f2585a4f42b92 to your computer and use it in GitHub Desktop.
An C# auxiliary class to easily create URLs with parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UrlFactory | |
{ | |
private Dictionary<string, string> _parameters; | |
private string _url; | |
public UrlFactory(string url) | |
{ | |
_url = url; | |
_parameters = new Dictionary<string, string>(); | |
} | |
public void AddParameter(string key, string value) | |
{ | |
_parameters.Add(key, value); | |
} | |
public string GetFullUrl() | |
{ | |
var url = _url; | |
if (_parameters.Count == 0) | |
return url; | |
var urlParameters = _parameters.Select(parameter => { | |
var key = WebUtility.UrlEncode(parameter.Key); | |
var value = WebUtility.UrlEncode(parameter.Value); | |
return $"{key}={value}"; | |
}); | |
return $"{url}?{string.Join("&", urlParameters)}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: