コード例 #1
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            //it was necessary to diverge from the book here and use
            //this tutorial to get seeding to work: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app-xplat/adding-model
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    // Requires using SportsStore.Models;
                    SeedData.Initialize(services);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: Temoxa/SportsStore
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext, AppIdentityDbContext dbIdentityContext)
        {
            SeedData.Initialize(dbContext);
            IdentitySeedData.EnsurePopulated(app);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseStatusCodePages(); //Добавляет коды ответов в http response
            app.UseSession();         //Позволяет службе сеансов автоматически ассоциировать запросы с сеансами
            app.UseAuthentication();  //Авторизация + аутентификация
            //app.UseIdentity(); //Устаревшее
            //app.UseMvcWithDefaultRoute(); //Стандартный роут в приложении MVC
            app.UseMvc(routes =>
            {
                //routes.MapRoute(
                //    name: null,
                //    template: "Adminka",
                //    defaults: new { Controller = "Admin", action = "Index" });

                routes.MapRoute(
                    name: null,
                    template: "{category}/Page{page:int}",
                    defaults: new { Controller = "Product", action = "List" });

                routes.MapRoute(
                    name: null,
                    template: "Page{page:int}",
                    defaults: new { Controller = "Product", action = "List" });

                routes.MapRoute(
                    name: null,
                    template: "{category}",
                    defaults: new { Controller = "Product", action = "List", page = 1 });

                routes.MapRoute(
                    name: null,
                    template: "",
                    defaults: new { Controller = "Product", action = "List", page = 1 });

                routes.MapRoute(
                    name: null,
                    template: "{controller}/{action}/{id?}");

                //routes.MapRoute(
                //    name: "pagination",
                //    template: "Products/Page{page}",
                //    defaults: new { Controller = "Product", action = "List" });

                //routes.MapRoute(
                //    name: "default",
                //    template: "{controller=Product}/{action=List}/{id?}");
            });

            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }