将不同类型的路径存储到变量中 [英] store different type of path into variable

查看:52
本文介绍了将不同类型的路径存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有两种类型的网址:wwww.myweb.com/aboutus和www.myweb.com/aboutus/who-we-are

因为你可以看到用户可以点击aboutus链接,网址将是www.myweb / aboutus

然后如果他点击who-we-are链接哪个是aboutus的一个子类别,然后网址将是来自服务器的这种方式(有一种关系)www.myweb.com/aboutus/who-we-are ...在表类别和子类别之间),你可以看到这是一个seo友好但这不是我想要的...现在我的问题是如何获得每个这些的文件名并将每个文件存储到一个变量。

让我举个例子,这就是我从url获取fileName并将其存储到变量中的方式:



string

Hi,

I have 2 type of url : wwww.myweb.com/aboutus and www.myweb.com/aboutus/who-we-are
as you can see a user can click on the aboutus link and the url will be www.myweb/aboutus
and then if he click on who-we-are link which is a subcategory of aboutus then the url will be
www.myweb.com/aboutus/who-we-are...by the way this is coming from the server(there is a kind of relationship between tables category and subcategory), and as you can see this is a seo friendly but this is not what I am looking for...now my question is how can I get a filename of each one these and store each one into a variable.
Let me give you an example, this is how I am getting the fileName from a url and store it into a variable:

string

PageName = Path.GetFileName(Request.Url.ToString());





你可以看到我根据他点击的链接,可以得到关于我们或者我们是谁。

now thi我的问题是如何获得不同的网址,就像他点击了我们之间的链接意味着我们将获得的网址www.myweb.com/aboutus/who-we-are



如何将它存储到不同的变量中:



string var1 =www.myweb.com

string var2 =aboutus

string var3 =who-we-are



as you can see I can get aboutus or who-we-are depending of the link he clicked.
now this my question how can I get different pieces of the url like once he clicked the who-we-are link means this the url we gonna get www.myweb.com/aboutus/who-we-are

how can I store this into different variable like this :

string var1= "www.myweb.com"
string var2= "aboutus"
string var3= "who-we-are"

推荐答案

尝试:

Try:
PageName = Request.Url.ToString();
string[] parts = PageName.Split('/');

然后您可以通过数组使用各个部分,或将数组元素复制到命名变量中:

You can then use the various parts via the array, or copy the array elements into named variables:

string domain = parts[0];
string page = parts[parts.Length - 1];





[edit] Path.GetFullPath已删除 - OriginalGriff [/ edit]



[edit]Path.GetFullPath removed - OriginalGriff[/edit]


这篇关于将不同类型的路径存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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