Path.Combine for URLs?

前端 未结 30 2090
不思量自难忘°
不思量自难忘° 2020-11-22 14:28

Path.Combine is handy, but is there a similar function in the .NET framework for URLs?

I\'m looking for syntax like this:

Url.Combine(\"http://MyUrl.         


        
相关标签:
30条回答
  • 2020-11-22 15:26

    I found UriBuilder worked really well for this sort of thing:

    UriBuilder urlb = new UriBuilder("http", _serverAddress, _webPort, _filePath);
    Uri url = urlb.Uri;
    return url.AbsoluteUri;
    

    See UriBuilder Class - MSDN for more constructors and documentation.

    0 讨论(0)
  • 2020-11-22 15:27

    Uri has a constructor that should do this for you: new Uri(Uri baseUri, string relativeUri)

    Here's an example:

    Uri baseUri = new Uri("http://www.contoso.com");
    Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
    

    Note from editor: Beware, this method does not work as expected. It can cut part of baseUri in some cases. See comments and other answers.

    0 讨论(0)
  • 2020-11-22 15:27

    I just put together a small extension method:

    public static string UriCombine (this string val, string append)
            {
                if (String.IsNullOrEmpty(val)) return append;
                if (String.IsNullOrEmpty(append)) return val;
                return val.TrimEnd('/') + "/" + append.TrimStart('/');
            }
    

    It can be used like this:

    "www.example.com/".UriCombine("/images").UriCombine("first.jpeg");
    
    0 讨论(0)
  • 2020-11-22 15:28

    Use this:

    public static class WebPath
    {
        public static string Combine(params string[] args)
        {
            var prefixAdjusted = args.Select(x => x.StartsWith("/") && !x.StartsWith("http") ? x.Substring(1) : x);
            return string.Join("/", prefixAdjusted);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:32

    My generic solution:

    public static string Combine(params string[] uriParts)
    {
        string uri = string.Empty;
        if (uriParts != null && uriParts.Any())
        {
            char[] trims = new char[] { '\\', '/' };
            uri = (uriParts[0] ?? string.Empty).TrimEnd(trims);
    
            for (int i = 1; i < uriParts.Length; i++)
            {
                uri = string.Format("{0}/{1}", uri.TrimEnd(trims), (uriParts[i] ?? string.Empty).TrimStart(trims));
            }
        }
    
        return uri;
    }
    
    0 讨论(0)
  • 2020-11-22 15:33

    There's already some great answers here. Based on mdsharpe suggestion, here's an extension method that can easily be used when you want to deal with Uri instances:

    using System;
    using System.Linq;
    
    public static class UriExtensions
    {
        public static Uri Append(this Uri uri, params string[] paths)
        {
            return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
        }
    }
    

    And usage example:

    var url = new Uri("http://example.com/subpath/").Append("/part1/", "part2").AbsoluteUri;
    

    This will produce http://example.com/subpath/part1/part2

    0 讨论(0)
提交回复
热议问题