Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Configure the middlewares, and the order of
        // middlewares are very important because as the request comes in, it is going to hand it to each middleware in this order.

        //Summary
        //Remember these three major points about Middleware in ASP.NET Core 1.0:

        //Middleware components allow you complete control over the HTTP pipeline in your app.
        //Among other things, Middleware components can handle authorization, redirects, headers, and even cancel execution of the request.
        //The order in which Middleware components are registered in the Startup file is very important.

        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              WorldContextSeedData seeder,
                              ILoggerFactory factory,
                              WorldContext context)
        {
            factory.AddDebug(LogLevel.Warning);

            // AutoMapper Initializer
            Mapper.Initialize(config =>
            {
                config.CreateMap <TripViewModel, Trip>().ReverseMap(); // To, From. ReverseMap create a map for both directions.
                config.CreateMap <StopViewModel, Stop> ().ReverseMap();
            });

            if (env.IsEnvironment("Development"))
            {
                app.UseDeveloperExceptionPage();
                factory.AddDebug(LogLevel.Information);             // Debug as a logger type, is actually in another package. Debug Window.
            }
            else
            {
                factory.AddDebug(LogLevel.Error);                   // write it out to the Debug Window.
            }

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("<html><body><h3>Hello World!</h3></body></html>");
            //});

            // another piece of middleware to add some standard default files. this piece of middleware is called app.UseDefailtfiles, when looking into
            // wwwroot directory it is going to automatically look for an index.html, index.htm etc

            // Now we are going to be using the MVC Index.cshtml as the default page NOT the index.html; that is why we commented out app.UseDefaultFiles();
            //app.UseDefaultFiles(); // need to be first than UseStaticFiles

            // to serve static file index.html. Every feature of the web server is optional.
            // To serve static file you need the app class with staticfile method. This is adding the middleware to server files to the web browser.
            app.UseStaticFiles(); // Middleware. the yellow bulb is called Quick Launch.

            app.UseIdentity();    // turn on ASP.NET Identity


            //app.UseDeveloperExceptionPage(); // display those errors. another piece of midlleware.

            // Enable to listne MVC operations.
            // take an URL and map it to a route, here we define the route to the controller.
            app.UseMvc(config =>
            {
                config.MapRoute(
                    name: "Default",
                    template: "{controller=App}/{action=Index}/{id?}"); //,  // PATTERN, the third part is an optional ID with the question mark
                //defaults: new { controller = "App", action = "Index" } // this will allow the root path mapped directly to that index method on the app controller.
                //);
            });     // Middleware

            context.Database.Migrate();
            //let's call it with Wait so it becomes a synchronous operation. I can't make configure an Asynch call, has to be synchronous, that is why the little trick of using Wait instead.
            seeder.EnsureDeedData().Wait();
        }