c# - Is there an UriAgilityPack / UrlAgilityPack? -


i'm trying figure out url/uri methodology asp.net manipulate uri/url.

it'd nice have:

uriagilitypack uri = new uriagilitypack("http://some.com/path?query1=value1"); 

or

uriagilitypack uri = new uriagilitypack().loadsiterelative("~/path?query1=value1"); 

then with

enum schemes {   http,   https }  uri.scheme = schemes.https; 

and/or

foreach(queryitem item in uri.query)    // item 

and/or

uri.query.add("addtocart", 5) 

string s = uri.tostring() // https://some.com/path?query1=value1&addtocart=5

or

string root = uri.rootpath                // /path?query1=value1&addtocart=5 string relative = uri.rootrelativepath;   // ~/path?query1=value1&addtocart=5 

no need agility pack that's built framework. start uri:

var uri = new uri("http://some.com/path?query1=value1"); 

and then:

console.writeline(uri.scheme); 

and then:

var nvc = httputility.parsequerystring(uri.query); foreach (string key in nvc) {     console.writeline("key: {0}, value: {1}", key, nvc[key]); } 

and then:

var builder = new uribuilder(uri); var nvc = httputility.parsequerystring(uri.query); nvc.add("addtocart", "5"); builder.query = nvc.tostring(); console.writeline(builder.tostring()); 

and finally:

console.writeline(builder.uri.pathandquery); 

see how easy is? , what's cooler can in desktop application, don't need asp.net @ all.

if had 1 advice give when dealing urls in .net never use string concatenations. use api guarantee proper encoding , respect of web standards.


Comments