Introduction to ASP.NET MVC
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.
An MVC Application is designed and implemented using the following three mechanisms:
· Model: MVC model is basically a C# or VB.NET class. A model is accessible by both controller and view. A model can be used to pass data from controller to view. A view can use model to display data in page.
· View: View is an ASPX page without having a code behind file. All page specific HTML generation and formatting can be done inside view. We can use inline code (server tags) to develop dynamic pages. A request to view (ASPX page) can be made only from a controller’s action method.
· Controller: Controller is basically a C# or VB.NET class which inherits System.Mvc.Controller. Controller is a heart of the entire MVC architecture. Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views. Controller can access and use model class to pass data to views. Controller uses ViewDatato pass any data to view.
Benefits of ASP.NET MVC
Enables the full control over the rendered HTML.
Provides clean separation of concerns(SoC).
Enables Test Driven Development (TDD).
Following the design of stateless nature of the web.
RESTful urls that enables SEO.
No ViewState and PostBack events
The main advantage of ASP.net Web Form are:
It provides RAD development
Easy development model for developers those coming from winform development.
Create Asp.net MVC 3 Application using C#
Here I am going to explain how to open and create a MVC Application in Visual Studio 2010. To create MVC Application:
1) Open Microsoft Visual Studio 2010
2) Click on New Project.
3) Select ASP.NET MVC 3 Web Application from New Project template.
4) You can change the name and location of Application from default name and location respectively. Here I gave a name of the application FirstMVCApp.
5) Click OK to proceed.
Next we have to select a project type from the project template. We can select either Empty or Internet Application from project template.
If you select Empty option it will not give you any sample code in application.
If you select Internet Application option it will give you sample code, provided by the ASP.NET default.
Next select View Engine from Dropdown list. There are two View Engine provided by ASP.NET MVC i.e. ASPX and Razor.
Razor uses C# or Visual Basic syntax for having code inside a page without ASP.NET delimiter: <%= %>. Razor file extension is ‘cshtml’ for C# language, and ‘vbhtml’ for Visual Basic whereas ASPX uses ASP.NET delimiter and ASPX file extension is ‘aspx’ for both C# language and Visual Basic.
Here I have selected an ASPX view for my application. After selecting a view engine click on OK button.
Then, Open Solution Explorer (Ctrl+Alt+L) right click on Controller folder and add a new controller.
When you select Add – Controller, Add controller dialog box will open. You can change the name of controller as I here change it to HomeController. Then click on Add button.
Then comment the Index method that return the View because we do not have an Index View right now and add your Index method that return the string.
public class HomeController : Controller
// GET: /Home/
/*public ActionResult Index()
public string Index()
return "My First MVC Application";
Press F5 to debug your application. You can see the output in your browser that display’s "My First MVC Application";