Create a Home page in ASP.Net Core 3.1 MVC

Since the basic project structure is now in place, we need to implement the different components that need to work together to provide the LearningAspNetCore web application:

Step 1 : Update the Startup.cs files

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                   name: "default",
                   pattern: "{controller=Home}/{action=Index}/{id?}"
                );
            });
        }
    }

  1. The first thing to do is to add MVC to the services configuration.
  2. you need to enable the use of static content within your ASP.NET Core
  3. Setting the routing path

Step 2 : Add a new controller, right-click within Solution Explorer on the Controllers folder, and then select Add | Controller…

Step 3 : In the Add Scaffold pop-up window, choose MVC Controller – Empty, and name your new controller HomeController

Step 4 : Your MVC home controller gets autogenerated, containing a single method. You now need to add a corresponding view by right-clicking on the Index method name and selecting Add View… from the menu:

Step 5 : The Add View window helps to define what needs to be generated. Leave the default empty template and enable the usage of the layout page.

Step 6 :  Congratulations! Your view gets autogenerated and you can test your application by pressing F5, or by clicking Debug on the Visual Studio 2019 menu and then Start Debugging

Add Comment