Exemplo n.º 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,
            ILoggerFactory loggerFactory,
            Entities.CityInfoContext cityInfoContext)
        {
            // No need to add these loggers in ASP.NET Core 2.0: the call to WebHost.CreateDefaultBuilder(args)
            // in the Program class takes care of that.
            loggerFactory.AddConsole();
            // loggerFactory.AddDebug();
            loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());

            // Exception middleware must be first
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler();
            }

            // Populate database with data if empty
            cityInfoContext.EnsureSeedDataForContext();

            // Return http status code and status text on error
            // to be available in a browser
            app.UseStatusCodePages();

            // Add Mvc middleware to the request pipeline
            app.UseMvc();

            //app.Run((context) =>
            //{
            //    throw new Exception("Example exception");
            //});

            // Answer for http://localhost
            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
        public static void EnsureSeedDataForDB(this CityInfoContext cityInfoContext)
        {
            if (cityInfoContext.Cities.Any())
            {
                return;
            }

            // init dummy data
            List <City> Cities = new List <City>()
            {
                new City()
                {
                    Name            = "New York City",
                    Description     = "The one with that big park.",
                    PointOfInterest = new List <PointOfInterest>()
                    {
                        new PointOfInterest()
                        {
                            Name        = "Central Park",
                            Description = "The most visited urban park in the United States."
                        },
                        new PointOfInterest()
                        {
                            Name        = "Empire State Building",
                            Description = "A 102-story skyscraper located in Midtown Manhattan."
                        },
                    }
                },
                new City()
                {
                    Name            = "Antwerp",
                    Description     = "The one with the cathedral that was never really finished.",
                    PointOfInterest = new List <PointOfInterest>()
                    {
                        new PointOfInterest()
                        {
                            Name        = "Cathedral of Our Lady",
                            Description = "A Gothic style cathedral, conceived by architects Jan and Pieter Appelmans."
                        },
                        new PointOfInterest()
                        {
                            Name        = "Antwerp Central Station",
                            Description = "The the finest example of railway architecture in Belgium."
                        },
                    }
                },
                new City()
                {
                    Name            = "Paris",
                    Description     = "The one with that big tower.",
                    PointOfInterest = new List <PointOfInterest>()
                    {
                        new PointOfInterest()
                        {
                            Name        = "Eiffel Tower",
                            Description = "A wrought iron lattice tower on the Champ de Mars, named after engineer Gustave Eiffel."
                        },
                        new PointOfInterest()
                        {
                            Name        = "The Louvre",
                            Description = "The world's largest museum."
                        },
                    }
                }
            };

            cityInfoContext.Cities.AddRange(Cities);
            cityInfoContext.SaveChanges();
        }