| | | 1 | | /******************************************************************************** |
| | | 2 | | * NanoRoutePrefixExtensions.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | namespace 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 => api |
| | | 24 | | /// .AddHandler("GET", "/health/", (context, _) => 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 => api |
| | | 52 | | /// .AddHandler("GET", "/health/", (context, _) => Results.Ok()) |
| | | 53 | | /// .AddHandler("GET", "/users/", (context, _) => Results.Ok())); |
| | | 54 | | /// </code> |
| | | 55 | | /// </example> |
| | | 56 | | public TBuilder AddPrefix(string pattern, Action<RouteScopeBuilder> configureRoutes) // child route scopes |
| | 1 | 57 | | { |
| | 1 | 58 | | Ensure.NotNull(routeScopeBuilder); |
| | 1 | 59 | | Ensure.NotNull(pattern); |
| | 1 | 60 | | Ensure.NotNull(configureRoutes); |
| | | 61 | | |
| | 1 | 62 | | configureRoutes |
| | 1 | 63 | | ( |
| | 1 | 64 | | routeScopeBuilder.CreatePrefix(pattern) |
| | 1 | 65 | | ); |
| | | 66 | | |
| | 1 | 67 | | return routeScopeBuilder; |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |