Пример #1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = services.GetRequiredService <ApplicationDbContext>();
                //context.Database.Migrate();

                // requires using Microsoft.Extensions.Configuration;
                IConfiguration config = host.Services.GetRequiredService <IConfiguration>();
                // Set password with the Secret Manager tool.
                // dotnet user-secrets set SeedUserPW <pw>

                string adminUserPWD = config["SeedUserPW"];

                try
                {
                    SeedUserAndRoles.SeedData(services, adminUserPWD).Wait();
                }
                catch (Exception ex)
                {
                    ILogger logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex.Message, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                              UserManager <ApplicationUser> userManager,
                              RoleManager <ApplicationRole> roleManager)
        {
            // caching response for middlewares
            app.UseResponseCaching();

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseSwagger();

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

                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Application Name Rest Api");
                    c.DefaultModelExpandDepth(0);
                    c.DefaultModelsExpandDepth(-1);
                    c.InjectStylesheet("/swagger-ui/custom.css");
                    c.RoutePrefix = string.Empty;
                });
            }
            else
            {
                app.UseHsts();
                app.UseSwaggerUI(c =>
                {
                    c.DefaultModelExpandDepth(0);
                    c.DefaultModelsExpandDepth(-1);
                    c.InjectStylesheet("/swagger-ui/custom.css");
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Application Name Rest Api");
                    c.RoutePrefix = string.Empty;
                });
            }

            var seed = new SeedUserAndRoles(userManager, roleManager);

            seed.SeedRoles().Wait();

            var option = new RewriteOptions();

            option.AddRedirect("^$", "swagger");
            app.UseRewriter(option);


            //app.UseCors();

            var options = app.ApplicationServices.GetService <IOptions <RequestLocalizationOptions> >();

            app.UseRequestLocalization(options.Value);

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "DefaultApi",
                    template: "application/webapi/v1/[controller]/[action]"
                    );
                routes.MapRoute(
                    name: "SecondaryApi",
                    template: "application/webapi/v1/[controller]"
                    );
            });
        }