Lightweight library for effortless URL construction from .NET objects. Simplifies object-to-query string conversion, ensuring flexibility and ease of use.
$ dotnet add package Net.UrlifyNet.Urlify is a lightweight and powerful NuGet package designed to simplify the construction of URLs with query parameters in .NET applications. It provides a clean and fluent way to append query string parameters to URLs using property attributes.
QueryStringPropertyAttribute, including control over the order in which parameters appear in the URL.Define Your Model
Start by decorating your model's properties with the QueryStringPropertyAttribute to specify which properties should be included as query string parameters, and optionally specify their order:
using Net.Urlify.Attributes;
public class MyModel
{
[QueryStringProperty("user", false, 2)]
public string Username { get; set; }
[QueryStringProperty("age", true, 1)]
public int Age { get; set; }
}
In the above model, Username and Age are marked as query parameters. Username will use "user" as the query parameter name and will not be URL-encoded. The order property ensures that "user" appears before "age" in the URL query string.
Inherit from Urlify
Inherits from Urlify and defines a base URL. This class will use the properties of your model to build the full URL.
public class MyModel : Urlify
{
[QueryStringProperty("user", false, 1)]
public string Username { get; set; }
[QueryStringProperty("age", true, 2)]
public int Age { get; set; }
public MyModel(string baseUrl) : base(baseUrl) { }
}
Construct the URL
Call the BuildUrl() method to construct your URL:
var model = new MyModel("http://example.com")
{
Username = "John Doe",
Age = 28
};
var url = model.BuildUrl(); // Outputs: http://example.com?user=John%20Doe&age=28