none
New to Visual Studio-Tutorial Question

    Question

  • Hello, just started with VS community 2015.  I've been going through the tutorial for it under creating a web application with C#.  The instructions have been fairly clear but I'm stuck at a point and was wondering if somebody could help out.   The link to the tutorial is https://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html

    I am at the following point of the tutorial: 

    Next, in Solution Explorer, open the Startup.cs file. Add the following line of code at the end of the Configure method:

                                  

    SampleData.Initialize(app.ApplicationServices);

    In the startup.cs file, I see a section called:

    // 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)

    I am guessing the tutorial is referring to this section, not positive. The rest of the code is:

                                  

    {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();

                app.UseApplicationInsightsRequestTelemetry();

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

                    // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                    try
                    {
                        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                            .CreateScope())
                        {
                            serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                                 .Database.Migrate();
                        }
                    }
                    catch { }
                }

                app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

                app.UseApplicationInsightsExceptionTelemetry();

                app.UseStaticFiles();

                app.UseIdentity();


                // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

            }

            // Entry point for the application.
            public static void Main(string[] args) => WebApplication.Run<Startup>(args);

    I've tried a few spots but it gives an error about not recognizing some of it.  Can somebody tell me where to put the code in? Thanks, Todd

    Friday, April 8, 2016 2:15 PM

Answers

  • Like this:

               

    {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();

                app.UseApplicationInsightsRequestTelemetry();

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

                    // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                    try
                    {
                        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                            .CreateScope())
                        {
                            serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                                 .Database.Migrate();
                        }
                    }
                    catch { }
                }

                app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

                app.UseApplicationInsightsExceptionTelemetry();

                app.UseStaticFiles();

                app.UseIdentity();


                // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

                SampleData.Initialize(app.ApplicationServices);

            }

    • Marked as answer by BigT2112 Monday, April 11, 2016 3:26 PM
    Friday, April 8, 2016 8:58 PM

All replies

  • That is the correct method and you should add it at the end after the app.UseMvc method call but could you let us know specifically what it doesn't like with the code?

    More than likely you are just missing a using statement.

    Friday, April 8, 2016 3:31 PM
  • So, enter like this?
    app.UseMvc(routes =>SampleData.Initialize(app.ApplicationServices);

    When I put SampleData.Initialize(app.ApplicationServices); into another line,

    it gives the following:  (sorry, my account won't let me paste images yet)

    Void SampleData.Initialize(IServiceProvider service provider)

    Friday, April 8, 2016 3:52 PM
  • Like this:

               

    {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();

                app.UseApplicationInsightsRequestTelemetry();

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

                    // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                    try
                    {
                        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                            .CreateScope())
                        {
                            serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                                 .Database.Migrate();
                        }
                    }
                    catch { }
                }

                app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

                app.UseApplicationInsightsExceptionTelemetry();

                app.UseStaticFiles();

                app.UseIdentity();


                // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

                SampleData.Initialize(app.ApplicationServices);

            }

    • Marked as answer by BigT2112 Monday, April 11, 2016 3:26 PM
    Friday, April 8, 2016 8:58 PM
  • Thank you, I am good to go! 
    Monday, April 11, 2016 3:26 PM