Equivalent function to IO.Path.GetFileName for urls?

前端 未结 4 1225
情歌与酒
情歌与酒 2021-01-11 13:54

In .NET 4.0, whats the equivalent function to IO.Path.GetFileName for urls?

相关标签:
4条回答
  • 2021-01-11 14:21

    You may stick to creating Uri object or if you care about performance use something similar to this one:

        public class UriHelpers
        {
            public static string GetFileNameFromUrl(string url)
            {
                string lastSegment = url.Split('/').Last();
    
                return lastSegment.Substring(0, lastSegment.IndexOf('?') < 0 ? lastSegment.Length : lastSegment.IndexOf('?'));
            }
        }
    
    0 讨论(0)
  • 2021-01-11 14:30

    You can use Server.MapPath() to map a physical path from a virtual path.

    Additionally, there are a number of methods within the HTTPUtility that will help you map various different types of path.

    0 讨论(0)
  • 2021-01-11 14:35

    There are many ways, the main being described here
    Baqsically exploiting the Uri class, and maybe string tokeniztion if needed.

    0 讨论(0)
  • 2021-01-11 14:37

    The Uri class is your friend.

    Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

    IsFile will try to determine if the Uri does indeed point to a file.

    Use the Segements property in order to get the file name (it will be the last segment).

    Uri uri = new Uri("http://example.com/title/index.htm");
    var filename = uri.Segments[uri.Segments.Length - 1];
    // filename == "index.htm"
    
    0 讨论(0)
提交回复
热议问题