Creating ASP.NET Core Application using MVC Template

Creating ASP.NET Core Application using MVC Template

In this article, I will discuss How to Create ASP.NET Core Web Applications using MVC (Model-View-Controller) Project Template and understand the Folder and File Structure. Please read our previous article before proceeding to this article, where we discussed ASP.NET Core Dependency Injection with Examples. 

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 Setting up the MVC Request pipeline in the 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 now onwards, in this ASP.NET Core MVC course, we will create the applications using the ASP.NET Core Web App (Model-View-Controller) Project template. 

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 click 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, the following Create a new Project window will open. From this window, select C#, All Platforms, and Web from the 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 image below.

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. You must provide the necessary information to create a new ASP.NET Core project here. 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 the Create button, the following Additional Information window will open: You must select Framework – .NET 6.0 (Long-term support), Authentication type – None. You must also check the Configure for HTTPS and do not use top-level statements check boxes. Finally, click the Create button, as shown in the image below.

Creating ASP.NET Core Application using MVC Template

Once you click on the Create Button, the project will be created with the Web Application (Model-View-Controller) template, i.e., the MVC template, with the following folder and file structure.

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

Understanding the ASP.NET Core MVC Project File and Folder Structure:

Several key components work together in an ASP.NET Core MVC application to ensure the application runs smoothly. Let’s discuss the roles and characteristics of each folder and file structure of the ASP.NET Core MVC application:

Dependencies Folder:

The Dependencies folder in ASP.NET Core stores the libraries, frameworks, and packages that your application relies on to function properly. These are managed through the csproj file or via the NuGet package manager. Common dependencies include ASP.NET Core libraries, Entity Framework Core, and third-party libraries for features like logging, authentication, etc. If we add new packages as per our requirement, those packages will be added inside this Dependencies folder (the Packages folder, which is a subfolder of Dependencies).

Properties Folder:

The Properties folder typically contains the launchSettings.json file. This file configures the settings used by Visual Studio to launch your application. It includes environment variables, application URL, and other settings related to how the application is run during development. You can specify different settings for different environments (like Development, Staging, and Production) in this file. 

wwwroot Folder:

The wwwroot folder is the root of the web server or project’s web root directory and contains all the static files your application serves directly to clients, such as HTML, CSS, JavaScript, and image files. Files in this folder are accessible via a URL, unlike other parts of your application. It is the default static file location, but you can configure ASP.NET Core to use different or additional directories.

Controllers Folder:

The Controllers folder contains classes that handle client requests and return a response. Each controller class is typically responsible for a specific area of the application. Controllers process incoming requests, perform operations on the data through the models, and select a view to render a response. The methods inside controller classes are called actions and mapped to different routes defined in the application.

Models Folder:

The Models folder contains classes representing the application’s data and the logic to manage that data. Models transfer data between the controllers and the views or represent the data that needs to be persisted in a database. They often correspond to database tables in applications that use Entity Framework Core. For instance, a Product model might represent a product table in a database.

Views Folder:

The Views folder contains files that are used to generate the HTML content returned to the client. These files are typically Razor view files (.cshtml), which mix HTML markup with C# code for dynamic content generation. Views are organized into subfolders, usually with a folder for each controller, and the view files correspond to actions in the controller.

Shared Folder:

The Shared folder in ASP.NET Core MVC stores shared resources like Razor layout pages (_Layout.cshtml), partial views, or other common components used across different views in your application. It helps maintain a consistent look and feel across your site and promotes the reusability of views and components. The Shared folder is typically located under the Views directory.

_ViewStart.cshtml File:

The _ViewStart.cshtml file defines common settings applied to all Razor views in the application (or in a specific folder if multiple _ViewStart.cshtml files are used). It’s executed before the rendering of each view. This makes it ideal for setting layout pages and other common settings.

ViewImports.cshtml File:

The _ViewImports.cshtml file includes common Razor directives that you want to be available in your views. It helps in reducing code duplication. Common uses of _ViewImports.cshtml include:

  • Importing namespaces so you don’t have to add @using directives to every view.
  • Adding Tag Helpers, a feature in ASP.NET Core that enables server-side code to create and render HTML elements in Razor files.
appsettings.json file
  • The appsettings.json file is used for configuration purposes. It stores configuration data in a JSON format, including database connection strings, application settings, logging configuration, and more.
  • The settings in appsettings.json can be environment-specific. For instance, you can use appsettings.Development.json, appsettings.Staging.json, and appsettings.Production.json to hold configurations for different environments. The appropriate file is selected based on the application’s runtime environment.
Program.cs File:
  • In ASP.NET Core MVC, Program.cs is the entry point of the application and is responsible for configuring and running the web application.
  • This file contains the Main method, which creates a host for the web application using a builder. The host configures the app’s services, logging, configuration sources, and more.
  • With .NET 6 and later, Program.cs has been simplified to use top-level statements, eliminating the need for a separate Startup.cs file. This results in more concise and readable code.
  • Services (like MVC, Entity Framework Core, and Identity) are configured here, and middleware is set up in the request processing pipeline.
ASP.NET Core MVC Setup:

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

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’s 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 will discuss ViewData in ASP.NET Core MVC with Examples. In this article, I explain how to Create an ASP.NET Core application using the 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 *