0

I have a URL ("http://localhost:2477/") on which I do get and post request. I have stored this URL in the app.config file of my project.

In the code, depending on the function, I add the string "getValue?id={0}" or "postValue" to this URL. But I later ran into an issue when I changed the URL to "http://localhost:2477" (no forward slash in the end) in the app.config.

Took me some embarrassing amount of time to figure out this issue, which made me wonder if there is a good way to handle this case.

Irrespective of the case when there is a forward slash or not in the URL, I want my code to change it to a proper URL.

Pranav Raj
  • 781
  • 1
  • 8
  • 19

3 Answers3

1

Always use Path.Combine(string, string). This method will conform a valid path and should add the / if needed.

edit I realized my answer does not work for URL, just for file paths.

What you’re looking for is Uri constructor instead.

Uri baseUri = new Uri("http://www.contoso.com");

Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Pablo Recalde
  • 3,334
  • 1
  • 22
  • 47
0

Using the Uri class you can modify your URL more elegantly. You can access the Host, Port, Query, etc. with ease. A similar question was asked here.

Cardi DeMonaco Jr
  • 536
  • 1
  • 7
  • 19
0

Try to use the UriBuilder, it's far more flexible as the Uri Constructor. See https://stackoverflow.com/a/20164328/10574963

samtrion
  • 111
  • 6