< Summary

Information
Class: NanoRoute.NanoRoutePrefixExtensions
Assembly: NanoRoute.dll
File(s): /home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Public/Extensions/NanoRoutePrefixExtensions.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 72
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
AddPrefix<TBuilder>(...)60

File(s)

/home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Public/Extensions/NanoRoutePrefixExtensions.cs

#LineLine coverage
 1/********************************************************************************
 2* NanoRoutePrefixExtensions.cs                                                  *
 3*                                                                               *
 4* Author: Denes Solti                                                           *
 5********************************************************************************/
 6using System;
 7
 8namespace NanoRoute
 9{
 10    using Internals;
 11
 12    /// <summary>
 13    /// Adds prefix-routing helpers that configure child route scopes under a shared base pattern.
 14    /// </summary>
 15    /// <remarks>
 16    /// <para>
 17    /// Prefix scopes inherit the value parsers and metadata visible when the child scope is created. Later
 18    /// changes made inside the prefix scope stay local to that child branch.
 19    /// </para>
 20    /// </remarks>
 21    /// <example>
 22    /// <code>
 23    /// builder.AddPrefix("/api/*", api =&gt; api
 24    ///     .AddHandler("GET", "/health/", (context, _) =&gt; Results.Ok()));
 25    /// </code>
 26    /// </example>
 27    public static class NanoRoutePrefixExtensions
 28    {
 29        extension<TBuilder>(TBuilder routeScopeBuilder) where TBuilder : RouteScopeBuilder
 30        {
 31            /// <summary>
 32            /// Creates a child route scope for the given prefix, invokes a configuration callback, and returns the curr
 33            /// </summary>
 34            /// <param name="pattern">
 35            /// The base prefix. It must be a valid route pattern ending in <c>/*</c> so child routes can be appended to
 36            /// </param>
 37            /// <param name="configureRoutes">A callback that configures routes on the child route scope.</param>
 38            /// <returns>The current builder.</returns>
 39            /// <exception cref="ArgumentNullException">
 40            /// Thrown when <paramref name="routeScopeBuilder"/>, <paramref name="pattern"/>, or
 41            /// <paramref name="configureRoutes"/> is <see langword="null"/>.
 42            /// </exception>
 43            /// <exception cref="ArgumentException">Thrown when <paramref name="pattern"/> does not end with <c>/*</c>.<
 44            /// <exception cref="ArgumentException">Thrown when <paramref name="pattern"/> has invalid route-template sy
 45            /// <exception cref="InvalidOperationException">
 46            /// Thrown when <paramref name="pattern"/> uses unsupported route-template features, references a
 47            /// missing value parser, or conflicts with an existing parser-backed branch.
 48            /// </exception>
 49            /// <example>
 50            /// <code>
 51            /// builder.AddPrefix("/api/*", api =&gt; api
 52            ///     .AddHandler("GET", "/health/", (context, _) =&gt; Results.Ok())
 53            ///     .AddHandler("GET", "/users/", (context, _) =&gt; Results.Ok()));
 54            /// </code>
 55            /// </example>
 56            public TBuilder AddPrefix(string pattern, Action<RouteScopeBuilder> configureRoutes)  // child route scopes 
 157            {
 158                Ensure.NotNull(routeScopeBuilder);
 159                Ensure.NotNull(pattern);
 160                Ensure.NotNull(configureRoutes);
 61
 162                configureRoutes
 163                (
 164                    routeScopeBuilder.CreatePrefix(pattern)
 165                );
 66
 167                return routeScopeBuilder;
 68            }
 69        }
 70    }
 71}
 72