3.28Setting Path Parameters (Template/Configuration Parameters)
Replaces object template strings in the URL path.
HttpRequestBuilder.Get("https://furion.net?id={id}&name={name}") .WithPathParameter("id", 1) // Adds a single parameter; {id} will be replaced with 1 .WithPathParameter("name", new[] { "furion", "monksoul" }) // Adds a single parameter; {name} will be replaced with furion,monksoul .WithPathParameters(new Dictionary<string, object?> { }) // Adds multiple path parameters .WithPathParameters(new { id = 1, name = "Furion" }) // Adds multiple path parameters; {id} and {name} are replaced with 1 and Furion respectively .WithPathParameters(new { id = 1, name = "Furion" }, "user") // Adds parameters with a prefix; {user.id} and {user.name} are replaced with 1 and Furion respectivelyHttpRequestBuilder.Get("https://furion.net/{id}/{name?}") // A trailing "?" means it is replaced with an empty string when the key does not exist .WithPathParameter(new { id = 1 });HttpRequestBuilder.Get("https://furion.net/{**path}") // A leading "**" means the path separator "/" is not escaped .WithPathParameter(new { path = "files/images/photo.jpg" });If a path parameter key is duplicated, the later key-value setting overrides the previous one.
Template Path Syntax
In addition to directly using {key}, template paths support accessing an object's properties and nested properties via ., and accessing elements in a collection via [index]. Furthermore, when a property of an object type is not found as a same-named property, the framework automatically tries to treat it as a dictionary and retrieves the value using the path identifier as the key (equivalent to dict["key"]).
{key}: Directly replaces the corresponding value.{key.property}: Accesses thepropertyproperty of thekeyobject, or whenkeyis a dictionary, accesses the value with the key"property".{key.property.nested}: Multi-level property/key access.{list[0]}: Accesses the element at index0in thelistcollection (arrays,List<T>, etc.).{user.names[1]}: First accesses thenamesproperty of theuserobject, then takes the element at index1.{dic.key}: Whendicis a dictionary (includingDictionary<string, T>andHashtable, etc.),dic.keyis resolved asdic["key"].{obj.dictProp.someKey[0].another}: Mixes dot and index notation to drill down level by level.
All of the above paths support appending ? at the end to indicate that the value is replaced with an empty string when it does not exist, and adding a ** prefix to indicate that the path separator / is not escaped.
Configuration Parameters
In addition to setting path parameters via the {key} template syntax, the framework also provides configuration parameters for reading configuration information and performing replacement. Configuration parameters use the [[key]] syntax, for example:
HttpRequestBuilder.Get("https://furion.net?id=[[id]]&name=[[name]]");Enabling Configuration Parameter Support
To enable configuration parameter support in the HttpRemote service, configure it as follows:
services.AddHttpRemote(builder => {}) .ConfigureOptions(options => { // Sets the provider source used to replace the configuration template parameters in the URL options.Configuration = builder.Configuration; // When using the Furion framework, you can directly set App.Configuration });Using Configuration Parameters
Configuration parameters are read from your configuration file and replaced into the URL. For example, your configuration file might look like this:
{ "id": 1, "name": "Furion"}Configuration parameter keys support multiple format syntaxes for more flexible access to values in the configuration file:
[[key]]: Directly accesses the value corresponding tokey.[[key:sub]]: Accesses the value of thesubsub-item underkey.[[key:sub:nest]]: Accesses the value of thenestsub-item within thesubsub-item underkey.- Fallback value lookup:
[[notfound | bak]]: Ifnotfounddoes not exist, looks upbak.[[notfound | bak | other]]: If neithernotfoundnorbakexists, looks upother.[[notfound | bak:sub | other:sub:nest]]: Supports deeper fallback lookups.
- Default values:
[[notfound || default]]: Ifnotfounddoes not exist, usesdefaultas the value.[[notfound | bak | other || default value]]: Combines fallback lookup and default values to ensure a value is always available.