articles

Home / DeveloperSection / Articles / Introduction to ASP.NET MVC

Introduction to ASP.NET MVC

Chris Anderson6933 29-Sep-2011

ASP.NET is a development framework for building web pages and web sites with HTML, CSS, JavaScript and server scripting.

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).
  • Easy integration with JavaScript frameworks.
  • 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.

Introduction to ASP.NET MVC

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.

Introduction to ASP.NET MVC

Then, Open Solution Explorer (Ctrl+Alt+L) right click on Controller folder and add a new controller.

Introduction to ASP.NET MVC

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.

Introduction to ASP.NET MVC

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace FisrtMVCApp.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        /*public ActionResult Index()
        {
            return View();
        }*/
 
        publicstring 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";

Introduction to ASP.NET MVC



Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By