C# Url Builder Class [closed]
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this questionI often en开发者_如何转开发d 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?
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.
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
We do use our own alternative Uri class that is partially based on Uri, as you say. However, I think there's an important distinction to be made - System.Uri is generally intended to be immutable - or, more precisely, behave immutably. Once one comes into existence, it represents a precise universal location/resource endpoint. If you need to describe a different location, you should create a new Uri, not change the existing one.
There's a separate class that specializes in producing Uri's: UriBuilder.
Between System.Uri
and System.UriBuilder
, what features exactly are you missing from those two?
/// <summary>
/// The arguments must be like: varName1, varValue1, varName2, varValue2 and so on.
/// Empty names will be not be added to the result.
/// Returns a string of the form varName1=varValue1&varName2=varValue2...
/// </summary>
public static string BuildQueryString(params string[] strings)
{
Debug.Assert(0 == strings.GetLength(0) % 2);
StringBuilder builder = new StringBuilder(50);
bool isName = true;
bool isEmptyName = false;
foreach (string crtString in strings)
{
isEmptyName = (isName && string.IsNullOrEmpty(crtString)) ||
(!isName && isEmptyName);
if (!isEmptyName)
{
builder.Append(HttpUtility.UrlEncode(crtString));
if (isName)
builder.Append("=");
else
builder.Append("&");
}
isName = !isName;
}
return builder.ToString();
}
There is a very nice new open source project on CodePlex that allows you to read, write, encrypt Uris and a bit more. Check them out!
http://querystrings.codeplex.com
I'm not sure exactly what you are trying to do (examples would help) but it sounds like you are trying to get some of the functionality of Apache's mod_rewrite in ASP.NET.
There is an article at MSDN about exactly this.
精彩评论