How to Create First MVC .Net Core Application ?

In this tutorial I am going to demonstrate about basic application creation in MVC Core and we will learn how to create Controller ,Action Methods and Views. Also we will learn how to display Model data into View and Run the application using Program.CS file.

Steps to be followed here are :-

  • Create a ASP.Net MVC Core Web App Project.
  • Create A Employee Model Class and define the Properties in it.
  • Create a Controller named it Employee Controller.
  • Create one Action Method Named it InsertData inside the Employee Controller and write the business logic here and return the data into view.
  • Create a View for InsertData Action Method.
  • Register the URL pattern into the Program.cs file.
  • Run the Application and See the desired output.

To start with create a ASP.Net MVC core web app project using the web app template as shown in below image and name the project then select for some default options and target framework as like in below images.

Visual Studio start window with Create a new project selected
Selecting the ASP.NET Core Web App (Model-View-Controller) template
Configuring the project name and location for the MVC app
Additional information dialog with .NET 8.0 and Configure for HTTPS selected
Solution Explorer with the Controllers, Models and Views folders

Install all the required packages from nuget package manager as shown in below image.

Installing the required NuGet packages

After installing the packages your solution explorer will be looks like as below image.

Solution Explorer showing the installed NuGet packages

Now Right Click and Controller folder and Add a Employee Controller as shown in below image.

Adding an Employee controller from the Controllers folder

Now Right Click on Model folder and add Employee Model class and add the below properties into it as shown in below code and image.

                    

 public int EmpId { get; set; }
 public string Name { get; set; }= string.Empty;
 public string Description { get; set; }= string.Empty;
 public string Address { get; set; }= string.Empty;
Employee model class with EmpId, Name, Description and Address properties

Now add one Action Method into Controller and name the Action method as "InsertData" and write the below code into it.

                    
 public class EmployeeController : Controller
    {
        public IActionResult InsertData()
        {
            Employee employee = new Employee();
            employee.EmpId = 1007;
            employee.Name = "Sudheer Singh Chouhan";
            employee.Address = "Bapat Square Indore";
            employee.Description = "Technical Project Manager";
            return View(employee);
        }
    }
InsertData action method returning an Employee object to the view

Now Right Click in View and add InsertData View as write the html code as like below image .

InsertData Razor view displaying the employee details

Now done the below changes in Program.cs file.

Default route changes in Program.cs

Now run the application you will get the output shown in below images.

Browser output of the first ASP.NET Core MVC application