コード例 #1
0
        public void Configure(IApplicationBuilder app, INodeServices nodeServices)
        {
            app.UseStaticFiles();
            app.UseWebSockets();
            app.UseSignalR();
            app.UseDotNetify();

            app.Run(async(context) =>
            {
                if (context.Request.Query["ssr"] == "false")
                {
                    // Client-side rendering.
                    using (var reader = new StreamReader(File.OpenRead("wwwroot/index.html")))
                        await context.Response.WriteAsync(reader.ReadToEnd());
                }
                else
                {
                    // Server-side reandering.
                    var path      = context.Request.Path.Value;
                    path          = path == "/" ? "/index" : path;
                    var ssrStates = ServerSideRender.GetInitialStates(ref path, typeof(Index));

                    var result = await nodeServices.InvokeAsync <string>("./src/app.server", path, ssrStates);
                    await context.Response.WriteAsync(result);
                }
            });
        }
コード例 #2
0
        /// <summary>
        /// Includes server-side rendering in the application request pipeline.
        /// </summary>
        /// <param name="appBuilder">Application builder.</param>
        /// <param name="mainVMType">Main application view model type.</param>
        /// <param name="nodeJSInvokeAsync">Function to invoke javascript in NodeJS.</param>
        /// <param name="onError">Request delegate to handle failure.</param>
        public static IApplicationBuilder UseSsr(this IApplicationBuilder appBuilder, Type mainVMType, Func <string[], Task <string> > nodeJSInvokeAsync, RequestDelegate onError)
        {
            var vmFactory = appBuilder.ApplicationServices.GetRequiredService <IVMFactory>();

            appBuilder.UseWhen(context => !Path.HasExtension(context.Request.Path.Value), app =>
            {
                app.Run(async context =>
                {
                    try
                    {
                        // Server-side rendering.
                        string path      = context.Request.Path.Value;
                        string ssrStates = ServerSideRender.GetInitialStates(vmFactory, ref path, mainVMType);

                        string userAgent = context.Request.Headers["User-Agent"];
                        string result    = await nodeJSInvokeAsync(new string[] { userAgent, path, ssrStates });
                        await context.Response.WriteAsync(result);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.Message);
                        await onError(context);
                    }
                });
            });
            return(appBuilder);
        }
コード例 #3
0
        /// <summary>
        /// Includes server-side rendering in the application request pipeline.
        /// </summary>
        /// <param name="appBuilder">Application builder.</param>
        /// <param name="mainVMType">Main application view model type.</param>
        /// <param name="nodeJSInvokeAsync">Function to invoke javascript in NodeJS.</param>
        public static IApplicationBuilder UseSsr(this IApplicationBuilder appBuilder, Type mainVMType, Func <string[], Task <string> > nodeJSInvokeAsync)
        {
            var vmFactory = appBuilder.ApplicationServices.GetRequiredService <IVMFactory>();

            appBuilder.UseWhen(context => !Path.HasExtension(context.Request.Path.Value), app =>
            {
                app.Run(async context =>
                {
                    // Server-side rendering.
                    string path      = context.Request.Path.Value;
                    string ssrStates = ServerSideRender.GetInitialStates(vmFactory, ref path, mainVMType);

                    string result = await nodeJSInvokeAsync(new string[] { path, ssrStates });
                    await context.Response.WriteAsync(result);
                });
            });
            return(appBuilder);
        }