问题描述
我需要从Request.UrlReferrer获取域名和文件夹名称。如果像http://example.com/folder1/folder2/page.aspx?query1=123&query2=456这样的网址意味着我需要获取http://example.com/folder1/folder2/或 folder1 / folder2 /,使用uri或其他标准方法。
我尝试过:
i need to get domain name and folder name from a Request.UrlReferrer. if the url like "http://example.com/folder1/folder2/page.aspx?query1=123&query2=456" means i need to get "http://example.com/folder1/folder2/" or "folder1/folder2/", by using uri or other standard method.
What I have tried:
string hostedFolder = urlReferrer.LocalPath.Replace(urlReferrer.Segments[urlReferrer.Segments.Length - 1], "");
推荐答案
string url = @"http://example.com/folder1/folder2/page.aspx?query1=123&query2=456";
Uri uri = new Uri(url);
然后 uri.LocalPath
会给你 /folder1/folder2/page.aspx
你可以使用 Path.GetDirectoryName(uri.LocalPath) )
只提供文件夹: \ folder1 \ folder2
Then uri.LocalPath
will give you /folder1/folder2/page.aspx
And you could use Path.GetDirectoryName(uri.LocalPath)
to give you just the folders: \folder1\folder2
这篇关于如何通过标准方式从客户端URL(request.urlreferrer)获取域名和文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!