Ejemplo n.º 1
0
        //Currently there are four pieces of middleware
        //IsDevelopment, CustomMiddleware, UseWelcomePage, Greetings
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              IGreetings greetings,
                              ILogger <Startup> logger)
        {
            //Middleware Configuration goes here
            //*** Sequence is important ***

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

            //To make sure that index.html is called as default file
            //we need to have this middleware in place
            //if this is not there then we need to call
            //sitename.com/index.html always this bypasses that
            // app.UseDefaultFiles();

            //To call static pages we need this middleware
            //This loads pages from wwwroot folder
            // app.UseStaticFiles();

            //replaces UseDefaultFiles and UseStaticFiles with one call
            app.UseFileServer();

            //Get Current environment
            var environment = env.EnvironmentName;

            //Custom Middleware for path /mym i.e. my middleware :)
            //if url is not /mym then pass along
            // app.Use(next => {
            //     return async context => {
            //         logger.LogInformation("Request Incoming");
            //         if (context.Request.Path.StartsWithSegments("/mym"))
            //         {
            //             await context.Response.WriteAsync("Hit!!");
            //             logger.LogInformation("Response Handled");
            //         }
            //         else
            //         {
            //             await next(context);
            //             logger.LogInformation("Response Outgoing");
            //         }
            //     };
            // });

            //Responds to request whose path would be /wp
            // app.UseWelcomePage(new WelcomePageOptions{
            //     Path = "/wp"
            // });

            app.Run(async(context) =>
            {
                // throw new Exception("Error");

                var greeting = greetings.GetGreetings();
                await context.Response.WriteAsync($"{greeting} : {environment}");
            });
        }
Ejemplo n.º 2
0
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              IGreetings greetings,
                              ILogger <StartupMvc> logger)
        {
            //*** Sequence is important ***

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

            //Get Current environment
            var environment = env.EnvironmentName;

            app.UseStaticFiles();
            // app.UseMvcWithDefaultRoute();
            app.UseMvc(ConfigureRoutes);

            app.Run(async(context) =>
            {
                // throw new Exception("Error");

                var greeting = greetings.GetGreetings();
                context.Response.ContentType = "text/plain";
                // await context.Response.WriteAsync($"{greeting} : {environment}");
                await context.Response.WriteAsync($"Page Not Found");
            });
        }
Ejemplo n.º 3
0
        //Return a View i.e. some view .cshtml instead of just a string or objectresult
        public IActionResult Index()
        {
            // var model = new Restaurant {
            //     Id = 1 , Name = "Over Story"
            // };
            var model = new HomeIndexViewModel();

            model.Restaurants    = _restaurantRepository.GetAll();
            model.CurrentMessage = _greetings.GetGreetings();

            return(View(model)); //Looks for Index.cshtml in Views folder
            // return View("Home"); // you can have different view name by default its the name of the Action
        }