Использование одностраничного приложения (Spa), преренеринг не возвращает правильный путь URL через IIS AspNetCoreModule - PullRequest
0 голосов
/ 08 февраля 2019

Мне удалось настроить одностраничный проект Angular с использованием приложения .NET Core Prerendering, и я могу подтвердить, что приложение Angular работает как на стороне сервера, так и на стороне клиента.

Я запускаю приложение в IIS 10, используяобработчик AspNetCoreModule, встроенный в производственный режим.Опять же, я могу подтвердить, что это работает.

У меня проблема с тем, что возвращается в контексте SpaPrendering каждый раз, когда страница перезагружается, в частности относительный путь.Он всегда возвращает /index.html, а не относительный путь в браузере.

Я пробовал это в IIS Express в Visual Studio, и относительный путь возвращает правильный результат.Так что, похоже, он изолирован от IIS и использует обработчик AspNetCoreModule.

public class Program
{
    public static void Main(string[] args)
    {
            CreateWebHostBuilder(args).Build().Run();

    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args).UseKestrel(options =>
        {
            options.Listen(IPAddress.Loopback, 5443); //HTTP port               
        })
            .UseStartup<Startup>();
}


public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist/ClientApp";
        });

        services.AddDbContext<DevResourceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevResourceDbContext")));

        services.AddScoped<ImageService>();
        services.AddScoped<EnquiryService>();
        services.AddScoped<CategoryService>();

        services.AddSingleton<RouteBackgroundService>();

        // Mapping
        services.AddAutoMapper();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        try
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //app.UseExceptionHandler("/Error");
                //app.UseHsts();                      
            }


            //app.UseHttpsRedirection();
            app.UseStaticFiles();                
            app.UseSpaStaticFiles();
            app.UseRewriter(new RewriteOptions().AddRedirect("index.html", "/"));

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";


                spa.UseSpaPrerendering(options =>
                {
                // If not working, make sure that npm install and npm install @angular-devkit/build-webpack have been ran.
                options.BootModulePath = $"ClientApp/dist-server/ClientApp/main.js";
                    options.BootModuleBuilder = env.IsDevelopment()
                        ? new AngularCliBuilder(npmScript: "build:ssr")
                        : null;
                    options.ExcludeUrls = new[] { "/sockjs-node" };

                    options.SupplyData = (context, data) =>
                    {

                        var routeBackgroundService = context.RequestServices.GetRequiredService<RouteBackgroundService>();
                        data["routes"] = JsonConvert.SerializeObject(new { Paths = routeBackgroundService.GetRouteData() }, Formatting.Indented);

                        // context.Request.Path always returns index.html, even if 
                        GetSupplyData(context, new Uri(string.Format("{0}://{1}{2}{3}", context.Request.Scheme, context.Request.Host, context.Request.Path, context.Request.QueryString)), data);
                    };
                });



                spa.Options.StartupTimeout = new System.TimeSpan(0, 3, 0);

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

    protected void GetSupplyData(HttpContext context, Uri uri, IDictionary<string, object> data)
    {
        var routeBackgroundService = context.RequestServices.GetRequiredService<RouteBackgroundService>();

        var path = uri.AbsolutePath;

        // Remove forward slash from path.
        if (path.Length > 0 && path.Substring(0, 1) == "/")
        {
            path = path.Substring(1, path.Length - 1);
        }

        // Does the path match a category?
        var selectedCategory = routeBackgroundService.GetCategory(path);

        if (selectedCategory != null)
        {
            data["selectedCategory"] = Map(context, selectedCategory);

            // Filter the image categories
            var imageCategories = routeBackgroundService.GetImageCategoryByCategory(selectedCategory.Id);

            // Now get the images.
            var images = routeBackgroundService.GetImageByCategory(imageCategories).Select(s => Map(context, s)).ToList();

            if (images != null)
            {
                data["images"] = JsonConvert.SerializeObject(new PageList<Image>(images, uri, uri.ToPageNumber() ?? 1, 12, 5), Formatting.Indented);
            }
        }

        // Ensure all querystring are added to the data.
        var queryData = System.Web.HttpUtility.ParseQueryString(uri.Query);

        foreach (var q in queryData.AllKeys)
        {
            data["querystring_" + q] = queryData[q];
        }
    }

    protected Image Map(HttpContext context, ImageEntity entity)
    {
        var mapper = context.RequestServices.GetRequiredService<IMapper>();

        return mapper.Map<ImageEntity, Image>(entity);
    }

    protected Category Map(HttpContext context, CategoryEntity entity)
    {
        var mapper = context.RequestServices.GetRequiredService<IMapper>();

        return mapper.Map<CategoryEntity, Category>(entity);
    }
}

Внутри класса StartUp существует пустота Configure.Внутри Configure void есть приложение.UseSpa, где вы можете настроить параметры.Внутри app.UseSpa я использую spa.UsePreRendering.

Когда страница загружается с сервера, она проходит через параметры .SupplyData внутри spa.UsePrerendering.Один из параметров внутри options.SupplyData - это контекст, который имеет тип HttpContext.

Однако, когда страница загружается с сервера, context.Request.Path всегда возвращает index.html, независимо от пути, введенного в браузере.Поэтому, если бы я положил http://domain/test,, я бы ожидал, что context.Request.Path будет показан / протестирован, но он всегда возвращает /index.html.Я предполагаю, что это происходит потому, что HTML-страницей по умолчанию для приложения Angular является /index.html.

Это происходит только в IIS 10 с использованием AspNetCoreModule и без использования IIS Express в Visual Studio.

...