5.10Setting Path Segments
Adds or removes URL path segments.
HTTP Declarative Requests set or remove path segments through the PathSegmentAttribute attribute. The corresponding HTTP declarative extractor is implemented by the PathSegmentDeclarativeExtractor type, which is responsible for parsing the PathSegmentAttribute attribute and building the path segment configuration required for the HttpRequestBuilder instance.
1. Adding Path Segments
Using the PathSegmentAttribute attribute, you can conveniently add path segments on an interface, method, or parameter.
// Applied on the interface definition, affects all methods[PathSegment("segment1")][PathSegment("segment2")]public interface IHttpService : IHttpDeclarative{ // Applied on the method [PathSegment("segment3")] [PathSegment("segment4")] [Get("https://furion.net/")] Task<string> GetStringAsync(); // Applied on a parameter, supports multiple specifications [PathSegment("segment3")] [Get("https://furion.net/")] Task<string> GetStringAsync([PathSegment] string segment3, [QueryParam][QueryParam] int lastSegment); // On a parameter, a default value can be set via the Segment property; it can also be set for the segment parameter, e.g. string? segment = "default" [Get("https://furion.net/")] Task<string> GetStringAsync([PathSegment(Segment = "default")] string? segment); // Frozen parameter types are ignored [Get("https://furion.net/")] Task<string> GetStringAsync([PathSegment] CancellationToken cancellationToken);}If duplicate path segments exist, they will appear repeatedly in subsequent appends (e.g., /docs/docs/users/docs/).
2. Removing Path Segments
In the PathSegmentAttribute attribute, setting Remove = true means removing that path segment. It is effective when applied on an interface, method, or parameter.
[PathSegment("segment1")] // Adds the segment1 path segment[PathSegment("segment2", Remove = true)] // Marks segment2 as pending removalpublic interface IHttpService : IHttpDeclarative{ [PathSegment("segment2")] // Adds the segment2 path segment [PathSegment("segment3")] // Adds the segment3 path segment [PathSegment("segment3", Remove = true)] // Marks segment3 as pending removal [Get("https://furion.net/")] Task<string> GetStringAsync([PathSegment(Remove = true)] string seg); // Dynamically marks seg as pending removal based on its value}Before sending the HTTP request, the set of path segments marked for removal specified in the configuration will be removed. In other words, the removal operation is performed after all setting operations have been invoked.
In the example above, although the GetStringAsync method attempts to add the segment2 and segment3 path segments via the [PathSegment] attribute, because the subsequent [PathSegment("segment2", Remove = true)] and [PathSegment("segment3", Remove = true)] attributes specify only the Remove = true property, these two keys are removed when the request URL is finally constructed. Only the segment1 path segment remains in the request URL.
PathSegmentAttribute contains the following constructors and properties:
-
Constructors:
new(): effective when applied to a parameter, adds a path segment whose value is the parameter value.new(segment): when applied to a method or interface, adds the specified path segment; when applied to a parameter whose value isnull, adds a path segment whose value is thesegmentparameter.
-
Properties:
Segment: the path segment (stringtype). When the attribute is applied to a parameter whose value isnull, it can be used as the default value.Remove: whether to mark it as pending deletion (booltype). The default value isfalse(append).