Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to extract the name of a particular directory of a URL and save it to a string from the server-side in ASP.NET C#.

For example, lets say I have a URL that looks like this:

http://localhost:1096/directory1/directory2/default.aspx

How would I get the value of directory1 from the URL and save it to a string variable?

Thanks in advance.
Posted
Comments
CodeBlack 12-Sep-13 7:21am    
what in case of URL routing ?
ZurdoDev 12-Sep-13 7:31am    
I'm not sure if there is a built in way; however, you could just always parse the string.

C#
string requestString = Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped);
string[] directories = requestString.Split(new char[] { '/' });

string firstSubDirectory = directories[1];
string secondSubDirectory = directories[2];
//etc...
 
Share this answer
 
I got the solution in the following page:
http://msdn.microsoft.com/en-us/library/system.uri.segments[^]
 
Share this answer
 

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