Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at How to setup MVC in ASP.NET Core. It's our 6th tutorial in ASP.NET Core series. So far, we have covered all the basic concepts in ASP.NET Core and are now ready to get our hands on MVC.
If you remember, when we were creating this project in Introduction to ASP.NET Core, we have selected Empty Template as we want to build our application from scratch. But if you want, you can also select MVC template and all these files, which we are going to create, will automatically be created in it. So, let's see How to setup MVC in ASP.NET Core:
How to setup MVC in ASP.NET Core ???
MVC in ASP.NET Core is an architectural design pattern, implemented by Microsoft.AspNet.Mvc package and composed of three components, which are:
When a user makes an HTTP request in the browser, this request is received by the Controller, which in turn targets the respective Model.
After being called by the controller, the model interacts with the database and also implements different operations on the data or request.
Controllers, not only select the respective Models, but also select the respective Views and pass on the data from Models to Views.
Here's a graphical representation of How MVC architecture works:
Now let's setup MVC in ASP.NET Core, so open up your Startup.cs file and in the ConfigureServices Method, we need to add required MVC Services.
This ConfigureServices Method is a Dependency Injection Container, so if we need to register any dependency in our application, we do it here.
As you can see in the figure on right side that we have added services.AddMvc() in our Dependency Injection Container.
Next, we need to add MVC Middleware in our Configure Method, so let's do it
As you can see in above code that I have added "MVC with Default Route" Middleware in my request processing pipeline.
When we add this MVC Middleware in our pipeline, by default it makes a search for Home Controller & then finds index Method in it.
So, now we have successfully setup our MVC Middleware and now it's time to understand its components one by one. So, first let's have a look at Controller.
Controllers in MVC
Controllers in MVC architecture are simple C# Classes, inherited from builtin Microsoft.AspNetCore.Mvc.Controller class and are used to handle incoming HTTP requests.
All public methods / functions in Controllers are normally referred as action methods i.e. we can invoke these methods using a specific URL and can perform required action / task.
Name of each Controller class is followed bu the word "Controller" i.e. if I want to create a Controller class named Home then it's file name will be HomeController.cs.
Now let's create a new folder titled Controllers in our Solution Explorer and add a new Class File in it named as HomeControllers.cs, as shown in figure on right side.
I have used MVC with default route Middleware, which has a default route for homepage and if you check its definition, then you will see that the default Controller is Home and the default action method is index.
So, now open this HomeController.cs class and add a new index Method in it, as shown in below figure:
As you can see in above code that I have added a new index Method in HomeController and that's the default home path for MVC Middleware.
So, now let's run our project and have a look at the browser and if everything goes fine then you will get this line printed on your homepage, instead of index.html file.
I hope you have understood the basics of Controllers, now let's have a look at Models in MVC architecture:
Models in MVC
Models in MVC architecture consists of different classes & interfaces which are requested by the Controllers and then perform different operations on the data objects.
Models are normally used to interact with the data sources i.e. getting or saving data & then this data is displayed on respective MVC View.
So,let's create a new folder named Models in our Solution Explorer and also add a new Class file in it named Engineers.cs, as shown in figure on right side.
As our site is related to Engineering, so I am going to create a new sign up form for engineers, so I have added below code in my Engineers.cs file:
So, we have created these properties in our Engineers Class and as you can see this Model class contains only our data.
We haven't added any Method to play with this data, so it's a data class.
Now we are going to create another class which will have all the Methods i.e. saving this data in database table or search for any data etc.
But before creating this class, let's first create its Interface in Models folder named IEngineersRepository.
In this interface, I have declared only one Method rite now but we will add a lot more in coming lectures, so here's it's code:
As you can see in above figure that have declared a method named GetEngineers(), which will get the Engineers data from database using its Id.
Now, let's create this second class, which will be implementing this Interface IEngineersRepository.
So, create a new class in Models folder and give it a name EngineersRepository, code is shown in below figure:
As you can see in above code that I have inherited this class from Interface IEngineersRepository, that's why it has the implementation of GetEngineers() function.
Before that Method Implementation, I have just added some hard coded data in the constructor of this class.
So, every time this class is instantiate, our data will be created, we will replace this data with SQL Database later but for now let's understand the MVC concept.
So, now we have two classes & one interface in Models folder.
In today's tutorial, we have a detailed overview of MVC architecture in ASP.NET Core. This tutorial isn't complete yet as we haven't yet discussed Views in MVC, but before discussing Views, we have to first understand Dependency Injection in ASP.NET Core, which is our next tutorial. After that, we will add Views in our project. Till then take care & have fun !!!
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow