This article is about What is MVC? and some important components about ASP .NET MVC.
I’ve started Open-Source E-Commerce project in ASP .NET MVC 5 you can read more about it here. Before moving forward,let’s understand about MVC.
The MVC stands for Model View Controller. Let’s discuss about MVC pattern in Web Application.
Why MVC is gaining popularity for Web Development?
The working of MVC Application is as follows:
1: User takes action and send request to application. (Controller)
2: The application then process data according to the request of user (Model)
3: The application then update view and send as response to the user (View)
Now let’s consider working of Web Application. User perform event on web-page,request is then sent to server (like HTTP request),server performs some operation and incorporate those changes in web-page and send that updated page to the user as response.
Now compare both working concept, we can say that working of MVC application and Web Application are somewhat same. That’s the main reason, Microsoft came up with ASP .NET MVC with improved separation of concern.
The MVC has three pieces:
- Models : Represent data that user works with. Mainly contains Entity class. Models doesn’t know anything about controller or view. So, we can use Models in any other application as well.
- Views : Represent the user interface, which will be rendered to represent models in UI.
- Controllers: Process request coming from user, performs operations on Models and select Views to render to user.
In ASP .NET MVC, Controllers and Models are C# Classes. Controllers class have method returning action method to return ActionResult objects which are view,content,partial view,JSON or empty model. Views are generated by View Engine of ASP .NET MVC Framework. MVC 3 introduced Razor view Engine, which uses different syntax for views.
The simple example is as follows (from E-CommerceMVC):
Apart from Model,View and Controller, In ASP .NET MVC there is one more component which is as important as these 3, viz. Router. The Router is responsible for where to send user request i.e Controller and Method of that controller.
The sample RouteConfig.cs is:
In ASP .NET default url is of form: Controller/Action/Id.
For above example the url will be: Product/Index
(Index is optional as default value for action is Index, And Index method of ProductController doesn’t need any parameter so ID is excluded from url).
We can specify the custom route in App_Start/RouteConfig.cs in ASP .NET MVC.
The output of above simple example is:
If you have any suggestions/queries please comment below.