Patrick
Patrick

Reputation: 5592

Path equivalent in asp.net (C#)?

Whats the equivalent to System.IO.Path ?

I got this url: http://www.website.com/category1/category2/file.aspx?data=123

How can i break this down, like

var url = ASPNETPATH("http://www.website.com/category1/category2/file.aspx?data=123");

url.domain <-- this would then return http://www.website.com

url.folder <-- would return category1/category2

url.file <-- would return file.aspx

url.queryString <-- would return the querystring in some format

Upvotes: 0

Views: 272

Answers (3)

Dave Swersky
Dave Swersky

Reputation: 34810

Use the UriBuilder class:
http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx

UriBuilder uriBuilder = new UriBuilder("http://www.somesite.com/requests/somepage.aspx?i=123");
string host = uriBuilder.Host;     // www.somesite.com
string query = uriBuilder.Query;   // ?i=123
string path = uriBuilder.Path;     // /requests/somepage.aspx

Upvotes: 7

Jesus Ramos
Jesus Ramos

Reputation: 23268

Regular expressions are perfect to use for this. Here's a link that has some nice info on using them. http://www.regular-expressions.info/

Edit : I forgot C# has a URI class which you can use for this as well.

Upvotes: 0

szeliga
szeliga

Reputation: 621

Check out the URI Class you can get all of that information using that class.

Upvotes: 4

Related Questions