Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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());


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"
Posted

1 solution

Try:
C#
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:
C#
string domain = parts[0];
string page = parts[parts.Length - 1];


[edit]Path.GetFullPath removed - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
CHill60 23-Mar-14 6:03am    
Beat me to it! 5'd
El Dev 23-Mar-14 6:44am    
Hi OriginalGriff,
what is the difference between :
string PagePath = Path.GetFullPath(Request.Url.ToString()); and
string PagePath = Request.Url.ToString();
because when I am using string PagePath = Path.GetFullPath(Request.Url.ToString());
I am getting an error that says "URI formats are not supported." but when I am using
string PagePath = Request.Url.ToString(); am getting the full path means it is working fine.
OriginalGriff 23-Mar-14 6:52am    
Use the Request.Url.ToString() method - Path.GetFullPath doesn't support web addresses (hence the message. I just copied the code from yours to save me typing it in and making a mistake - I assumed you had tested your code before posting it here... :sigh:

You use Path.GetFullPath to convert between an relative path "../test.txt", or a default path "test.txt" and an absolute path "D:\Temp\test.txt". You don't need it here, particularly since you aren't giving it an actual file anyway! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900