Home > Backend Development > C++ > How Can I Combine URLs Like File Paths in C#?

How Can I Combine URLs Like File Paths in C#?

Linda Hamilton
Release: 2025-01-20 13:26:09
Original
578 people have browsed it

How Can I Combine URLs Like File Paths in C#?

Combining URLs in C# – A Path.Combine Equivalent?

C#'s Path.Combine simplifies file path concatenation. Is there a comparable method for URLs? Will Url.Combine("http://MyUrl.com/", "/Images/Image.jpg") produce "https://www.php.cn/link/5499e63224605f2eb13406af0af5b76d"?

The Solution: Utilizing the Uri Class

The Uri class offers a constructor designed for this purpose:

new Uri(Uri baseUri, string relativeUri)
Copy after login

Example Implementation:

Uri baseUri = new Uri("http://www.contoso.com");
Uri combinedUri = new Uri(baseUri, "catalog/shownew.htm");
Copy after login

Important Considerations:

This approach, while seemingly straightforward, has limitations. In specific cases, it might unexpectedly shorten parts of the baseUri. For a complete understanding, consult the comments and alternative solutions provided in the original source.

The above is the detailed content of How Can I Combine URLs Like File Paths in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template