In this article, we will learn what is MVC and how to create your first ASP.NET MVC project in .NET 6 using visual studio. This article is targeted for beginners who are new to .NET, .NET 6, and MVC.
What is MVC (Model View Controller)?
It is a design pattern to decouple or segregate the user interface (called View), data(model), and application logic(controller).
- View is referred as User interface. User directly interact with the view and sends data to the controller or receive data from the controller.
- Controller communicates to the model and the view.
- Controller communicates with view and gets the data from user interface and sent it to the database model.
- It communicates with model, retrieves data from the database and pass it to the user interface(view). It’s a bridge to communicate with Model and View.
- We can create model classes and bind them to the database.
Creating First Project
Step 1 – Open Visual Studio and create a project.
Step 2 – Select ASP.Net Core Web App (Model-View-Controller) and click Next as depicted below.
Step 3 – Give Project Name and location of the project and click on Next.
Step 4 – Select Framework: .NET 6.0 (Long-term support) and click on Create.
It may take a few seconds to create a project. When it is created, it will look like.
In the default project, your folder structure will be like below.
Controller– This folder contains all the controllers.
Models– contains the model class related to the database. You need to create one model class having the property of one table from the database.
Views-It contains the. cshtml page which is the user interface. Each Controller has its own folder under the View such as we have HomeController and you can see the Home folder under Views.
The shared folder under Views contains the shared design that is needed for several pages. For example _Layout.cshtml contains the header and footer part of the page which is required for all pages. You can design your own custom shared view and use it wherever it is needed.
Now, Let’s run it. To run it you can click on the run option as shown below or press Ctrl+F5.
When you click the run option or view in browser option then it will look like below.
You can design and add more pages and customize the design according to your need.
Hence, we have learned what is MVC and how to create an ASP.NET Core MVC project in .NET 6.