如何通过标准方式从客户端URL(request.urlreferrer)获取域名和文件夹名称 [英] How to get domain name and folder name from client url (request.urlreferrer) by standard way

查看:496
本文介绍了如何通过标准方式从客户端URL(request.urlreferrer)获取域名和文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从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], "");

推荐答案

您可以使用Uri类:

You could use the Uri class:
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


Request.ApplicationPath 应该为您提供Web根相对逻辑路径。此外,如果您想处理URL中的某些段,那么您可能希望使用 string.Split 方法执行简单的字符串操作。
Request.ApplicationPath should give you the web root-relative logical path. Also, if you want to work on certain segments from the URL, then you might want to do a simple string manipulation instead using string.Split method.


这篇关于如何通过标准方式从客户端URL(request.urlreferrer)获取域名和文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆