Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Url Builder Class [closed]

Tags:

c#

asp.net

I often end up rolling my own wrapper/extension methods for/around System.Uri and was wondering if anyone knows of a good open source implementation. What I like to do most is parse querystring parameters, build a new query string (this is the key), and replace page names. Got any good ones, or is System.Uri good enough for you?

like image 544
Trent Avatar asked Nov 18 '09 23:11

Trent


2 Answers

BradVin's QueryString Builder class is good. Fluent interface and support for encryption.

It's also worth checking out this UrlBuilder class on CodeProject. Similar to System.UriBuilder has better support for working with the QueryString.

like image 188
Chris Fulstow Avatar answered Sep 22 '22 08:09

Chris Fulstow


Flurl [disclosure: I'm the author] is a fluent URL builder that looks like this:

var url = "http://www.some-api.com"
    .AppendPathSegment("endpoint")
    .SetQueryParams(new {
        api_key = ConfigurationManager.AppSettings["SomeApiKey"],
        max_results = 20,
        q = "Don't worry, I'll get encoded!"
    });

If you happen to be building URLs for the purpose of calling them, Flurl.Http is a companion lib that lets you do HTTP off the fluent chain:

await "https://api.mysite.com"
    .AppendPathSegment("person")
    .SetQueryParams(new { ap_key = "my-key" })
    .WithOAuthBearerToken("MyToken")
    .PostJsonAsync(new { first_name = firstName, last_name = lastName });

Get the full package on NuGet:

PM> Install-Package Flurl.Http

or just the stand-alone URL builder:

PM> Install-Package Flurl

like image 32
Todd Menier Avatar answered Sep 20 '22 08:09

Todd Menier