public static IEndpointRouteBuilder MapCocoonProxyWithBlazor(this IEndpointRouteBuilder endpoints, IEnumerable <string> blazorPaths)
        {
            var cocoonProxy = endpoints.ServiceProvider.GetRequiredService <CocoonProxy>();

            var blazorRoutes = new BlazorRoutes(blazorPaths);

            var app = endpoints.CreateApplicationBuilder();

            app.Use(async(httpContext, next) =>
            {
                if (blazorRoutes.Contains(httpContext.Request.Path))
                {
                    httpContext.Request.Path = "/index.html";

                    // Set endpoint to null so the static files middleware will handle the request.
                    httpContext.SetEndpoint(null);

                    await next();
                    return;
                }

                await cocoonProxy.ProxyAsync(httpContext);
            });

            app.UseStaticFiles();

            var func = app.Build();

            endpoints.MapFallback("/{**catch-all}", func);

            return(endpoints);
        }
        public static IApplicationBuilder UseExplicitBlazorRoutes(this IApplicationBuilder app, IEnumerable <string> blazorRouteTemplates)
        {
            var routes = new BlazorRoutes(blazorRouteTemplates);

            app.Use((context, next) =>
            {
                if (routes.Contains(context.Request.Path))
                {
                    context.Request.Path = "/";
                }

                return(next());
            });
            return(app);
        }
Example #3
0
        public void MatchesTemplateWithParameters()
        {
            string[] templates =
            {
                "/",
                "/Admin/Edit/{id}",
                "/Products/{*path}",
                "/Cats/{id}/Breed",
                "/Cats/{id}",
            };
            var target = new BlazorRoutes(templates);

            Assert.True(target.Contains("/admin/edit/42"));
            Assert.True(target.Contains("/products/foo/bar/quux"));
            Assert.True(target.Contains("cats/42/breed"));
            Assert.True(target.Contains("cats/42"));
            Assert.True(target.Contains("/"));

            Assert.False(target.Contains("/admin/edit/42/wibble"));
            Assert.False(target.Contains("/fail"));
        }