Creating ASP.NET Core Application using MVC Template

Creating ASP.NET Core Application using MVC Template

In this article, I am going to discuss How to Create an ASP.NET Core Web Application using MVC (Model-View-Controller) Project Template. Please read our previous article before proceeding to this article, where we discussed ASP.NET Core Dependency Injection with Examples. We will see the project layout of the ASP.NET 6 Core MVC application.

Creating an ASP.NET Core MVC Application:

As of now, we have discussed everything using the ASP.NET Core Empty Project Template and understand the different parts of an ASP.NET Core MVC Application. We also discussed How to set up the MVC Request pipeline in ASP.NET Core Web Application.

But the question is, do we need to manually set up everything to create an ASP.NET Core MVC Application? The answer is NO. The ASP.NET Core Framework provides one built-in project template called ASP.NET Core Web App (Model-View-Controller), which will create an ASP.NET Core MVC Application for us with the required MVC setup. From this session onwards, in this ASP.NET Core MVC course, we are going to create the applications using the ASP.NET Core Web App (Model-View-Controller) Project template. Let us see the step-by-step procedure to create the same.

Creating an ASP.NET Core Application using the MVC Project Template:

To create an ASP.NET Core Web Application with the MVC Project template. First, open Visual Studio 2022 and then click on the Create a new project tab, as shown in the image below.

Creating an ASP.NET Core Application using the MVC Project Template

Once you click on the Create a new Project tab, it will open the following Create a new Project window. From this window, select C#, All Platforms, and Web from respective dropdowns as highlighted below. Select “ASP.NET Core Web App (Model-View-Controller)” as highlighted below, and click on the “Next” button as shown in the below image.

Creating an ASP.NET Core Application using the MVC Project Template

Once you click on the Next button, it will open the Configure Your New Project window. Here, you need to provide the necessary information to create a new ASP.NET Core project. First, give an appropriate name for your project (SampleMVCWeb), set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Create button, as shown in the image below.

Creating ASP.NET Core Application using MVC Template

Once you click on the create button, it will open the following Additional Information window. Here, you need to select Framework – .NET 6.0 (Long-term support), Authentication type – None. You also need to check the Configure for HTTPS and Do not use top-level statements check boxes, and finally, click on the Create button as shown in the below image.

Creating ASP.NET Core Application using MVC Template

That’s it. Once you click on the Create Button, the project is going to be created with the Web Application (Model-View-Controller), i.e., MVC template with the following folder and file structure.

How to Create an ASP.NET Core Web Application using MVC Template (Model-View-Controller)

Let us understand the project structure in detail.

  1. Dependencies: This folder includes the required dependencies, i.e., the required packages to run ASP.NET Core Application. If we add new packages as per our requirement, those packages will be added inside this Dependencies folder (Packages folder, which is a sub-folder of Dependencies).
  2. Properties: This folder contains the launchsettings.json file and will only be used in the development environment. This will not be available when we publish the project and hence not available in any other environment.
  3. wwwroot: It is the project’s web root directory. The wwwroot folder going to contains all the static files such as .css, .js, and bootstrap files, etc.
  4. Controllers: All the controller classes are in the Controllers folder.
  5. Models: All the model classes go into the Models folder.
  6. Views: Contains Razor views (.cshtml) files for rendering HTML content. Razor is a view engine used in ASP.NET to create dynamic HTML
  7. Shared: Shared folder under the Views folder contains _Layout.cshtml. It is the default layout for the ASP.NET core app. It includes app header, footer, and navigation.
  8. appsettings.json: This file will contain the settings which we want throughout the application, such as connection string, application scope global variables, etc.
  9. Program.cs: The Program class contains the Main method, the entry point to our ASP.NET Core Web Application. The Main method is responsible for setting the web host, configuring the services (Both Built-in and User-Defined), configuring the Middleware Components (Both Built-in and User-Defined), and starting the application so that the application can listen to the HTTP Incoming Requests.
MVC Setup:

The ASP.NET Core Web APP (Model-View-Controller) Project template, by default, includes the required setup for MVC. To confirm this, open the Program.cs class file, and you will see the required MVC Services and MVC Request processing pipeline are added by the framework, as shown in the below image.

namespace SampleMVCWeb
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add MVC Services to the container.
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            //MVC Request Processing Pipeline
            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}
Running the MVC Application:

The ASP.NET Core Web APP (Model-View-Controller) Project template creates the Home Controller with some views. Let us run the application and see the output as shown below.

How to Create an ASP.NET Core Web Application using MVC Template (Model-View-Controller)

In the next article, I am going to discuss ViewData in ASP.NET Core MVC with Examples. In this article, I explain how to Create an ASP.NET Core application using ASP.NET Core Web APP (Model-View-Controller) Project template. I hope you enjoy Creating an ASP.NET Core MVC Application using the Model-View-Controller Project Template article.

Leave a Reply

Your email address will not be published. Required fields are marked *