Пример #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              Contracts.ILoggerManager logger,
                              RoleManager <IdentityRole> spravceRoli,
                              UserManager <IdentityUser> spravceUzivatelu,
                              IServiceProvider serviceProvider
                              )
        {
            serviceProvider.GetService <ApplicationDbContext>();


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }



            app.ConfigureExceptionHandler(logger);
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                //c.EnableApiKeySupport("X-ApiKey", "header");
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                //c.RoutePrefix = string.Empty;
            });
            //    spravceRoli.CreateAsync(new IdentityRole("admin")).Wait();
            //   IdentityUser uzivatel = spravceUzivatelu.FindByEmailAsync("*****@*****.**").Result;
            //    spravceUzivatelu.AddToRoleAsync(uzivatel, "admin").Wait();

            //   using (var context = app.ApplicationServices.GetService<ApplicationDbContext>())
            // {
            //    context.Database.EnsureDeleted();
            //    context.Database.EnsureCreated();
            // }
        }
        public static void ConfigureExceptionHandler(this IApplicationBuilder app, Contracts.ILoggerManager logger)
        {
            app.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "application/json";

                    var contextFeature = context.Features.Get <IExceptionHandlerFeature>();
                    if (contextFeature != null)
                    {
                        logger.LogError($"Something went wrong: {contextFeature.Error}");

                        await context.Response.WriteAsync(new ErrorDetails()
                        {
                            StatusCode = context.Response.StatusCode,
                            Message    = "Internal Server Error."
                        }.ToString());
                    }
                });
            });
        }