Thursday, February 25, 2010

Technical: ASP.NET MVC Architecture

Hi Friends,
I want to share with you my knowledge of ASP.NET MVC.
This is the new technology Microsoft had launched, which uses HTML Controls instead of ASP Server controls.

Switching back from ASP.NET to ASP.NET MVC was little tough for me, since we have to use HTML controls & JQuery for the client-server communication.

MVC stands for Model - View - Controller.
Models consists of classes which wroks in backend.
View mainly consists of UI pages i.e. ASPX & ASCX pages.
Controller is similar to code-behind pages in ASP.NET. It will consists of code behind logic.

For implementing ASP.NET MVC, we must know:
- JavaScript,
- JQuery,
- HTML Controls
- C#/VB.NET

One more interesting feature of ASP.NET MVC is URL Routing. Global.asax file consists of MapRoutes. Whenever a URL is fired, the map routes are checked & accordingly the action will be taken.

When you create an ASP>NET MVC Application, the Visual Studio already gives you folder structure, mainly consists of Models, Views & Controllers. Also it will give 'Global.asax' file.

Views
Views folder will have sub folders. For example: Home. The subfolders will consists of ASPX or ASCX pages. These pages consists of HTML controls.
For Example: Html.ActionLink, Button, etc.

Controllers
Controller name will be consists of subfolder name of Views, and consists of methods to handle events of the UI pages in that perticular subfolder.
For Example: HomeController will contain methods like: getPersonClicked(), btnHomeClicked(), etc.

Models
Models will consists of .cs files mainly consists of classes.
For example: Person class with its properties PersonId, Name, Age, etc.

Repository
Repository filder will consists of operations on classes in Models folder.
For example: PersonRepository class will contain methods like AddPerson(), GetPerson(), etc.

One important point to be noted is: "We can not access controller(its methods) directly from view and also we can not access controls in views directly from controller.!!!".
For this purpose we will have to use JQuery & ViewModel.

For accessing controller method from View, we will use JQuery. [$.ajax calls] and for passing the values from Controller to Model, we will use ViewModel.

Feeling very happy to share my knowledge with others.

Thanks.
Happy Coding..!!

No comments:

Post a Comment