0% found this document useful (1 vote)
9 views

Mvc Framework Tutorial

The document is a tutorial on the Model-View-Controller (MVC) framework, aimed at .NET programmers looking to learn about MVC architecture and its components. It covers essential topics including MVC components, ASP.NET MVC features, application architecture, and practical coding examples. The tutorial assumes a basic knowledge of ASP.NET and provides hands-on experience to help readers reach an intermediate level of understanding in MVC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
9 views

Mvc Framework Tutorial

The document is a tutorial on the Model-View-Controller (MVC) framework, aimed at .NET programmers looking to learn about MVC architecture and its components. It covers essential topics including MVC components, ASP.NET MVC features, application architecture, and practical coding examples. The tutorial assumes a basic knowledge of ASP.NET and provides hands-on experience to help readers reach an intermediate level of understanding in MVC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MVC Framework

About the Tutorial


As per the official definition, Model-View-Controller (MVC) is a software architectural
pattern for implementing user interfaces. It divides a given software application into three
interconnected parts, so as to separate internal representations of information from the
ways that information is presented to or accepted from the user.

Audience
This tutorial is targeted for .NET programmers beginning to learn MVC framework. This
tutorial will bring you to intermediate level of knowledge in MVC, covering all the important
aspects of MVC Framework with complete hands-on code experience.

Prerequisites
Before proceeding with this tutorial, we assume the readers have a basic knowledge of
ASP.NET development (C# and VB language) and Visual Studio software installed on their
system.

Disclaimer & Copyright


 Copyright 2017 by Tutorials Point (I) Pvt. Ltd.

All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.

We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected].

i
MVC Framework

Table of Contents
About the Tutorial .................................................................................................................................. i

Audience ................................................................................................................................................ i

Prerequisites .......................................................................................................................................... i

Disclaimer & Copyright ........................................................................................................................... i

Table of Contents .................................................................................................................................. ii

MVC FRAMEWORK – INTRODUCTION ................................................................................ 1

MVC Components.................................................................................................................................. 1

ASP.NET MVC ........................................................................................................................................ 2

MVC FRAMEWORK – ARCHITECTURE ................................................................................. 3

MVC FRAMEWORK – ASP.NET FORMS ............................................................................... 5

MVC FRAMEWORK – FIRST APPLICATION .......................................................................... 7

Create First MVC Application ................................................................................................................. 7

MVC FRAMEWORK – FOLDERS......................................................................................... 15

Controllers Folder ................................................................................................................................ 17

Models Folder ..................................................................................................................................... 18

Views Folder ........................................................................................................................................ 18

App_Start Folder ................................................................................................................................. 19

Content Folder..................................................................................................................................... 20

Scripts Folder ....................................................................................................................................... 20

MVC FRAMEWORK – MODELS ......................................................................................... 21

Create Database Entities ..................................................................................................................... 21

Generate Models Using Database Entities ........................................................................................... 22

ii
MVC Framework

MVC FRAMEWORK – CONTROLLERS ................................................................................ 27

Create a Controller .............................................................................................................................. 27

Create a Controller with IController .................................................................................................... 28

MVC FRAMEWORK – VIEWS............................................................................................. 30

Strongly Typed Views .......................................................................................................................... 31

Dynamic Typed Views .......................................................................................................................... 34

MVC FRAMEWORK – LAYOUTS ........................................................................................ 37

Create MVC Layouts ............................................................................................................................ 37

MVC FRAMEWORK ─ ROUTING ENGINE ........................................................................... 44

MVC Routing URL Format .................................................................................................................... 44

Create a Simple Route ......................................................................................................................... 45

MVC FRAMEWORK – ACTION FILTERS.............................................................................. 47

Types of Filters .................................................................................................................................... 47

Action Filters ....................................................................................................................................... 47

MVC FRAMEWORK – ADVANCED EXAMPLE ..................................................................... 50

Create an Advanced MVC Application ................................................................................................. 50

MVC FRAMEWORK – AJAX SUPPORT................................................................................ 74

Create an Unobtrusive Ajax Application .............................................................................................. 75

MVC FRAMEWORK – BUNDLING ...................................................................................... 80

Enable Bundling and Minification ........................................................................................................ 80

MVC FRAMEWORK – EXCEPTION HANDLING ................................................................... 82

Override OnException Method ............................................................................................................ 82

HandleError Attribute .......................................................................................................................... 85

iii
MVC Framework – Introduction

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.

MVC Components
Following are the components of MVC:

Model
The Model component corresponds to all the data-related logic that the user works with.
This can represent either the data that is being transferred between the View and
Controller components or any other business logic-related data. For example, a Customer
object will retrieve the customer information from the database, manipulate it and update
it data back to the database or use it to render data.

View
The View component is used for all the UI logic of the application. For example, the
Customer view will include all the UI components such as text boxes, dropdowns, etc. that
the final user interacts with.

Controller
Controllers act as an interface between Model and View components to process all the
business logic and incoming requests, manipulate data using the Model component and
interact with the Views to render the final output. For example, the Customer controller

1
MVC Framework

will handle all the interactions and inputs from the Customer View and update the database
using the Customer Model. The same controller will be used to view the Customer data.

ASP.NET MVC
ASP.NET supports three major development models: Web Pages, Web Forms and MVC
(Model View Controller). ASP.NET MVC framework is a lightweight, highly testable
presentation framework that is integrated with the existing ASP.NET features, such as
master pages, authentication, etc. Within .NET, this framework is defined in the
System.Web.Mvc assembly. The latest version of the MVC Framework is 5.0. We use Visual
Studio to create ASP.NET MVC applications which can be added as a template in Visual
Studio.

ASP.NET MVC Features


ASP.NET MVC provides the following features:

 Ideal for developing complex but lightweight applications.

 Provides an extensible and pluggable framework, which can be easily replaced and
customized. For example, if you do not wish to use the in-built Razor or ASPX View
Engine, then you can use any other third-party view engines or even customize the
existing ones.

 Utilizes the component-based design of the application by logically dividing it into


Model, View, and Controller components. This enables the developers to manage
the complexity of large-scale projects and work on individual components.

 MVC structure enhances the test-driven development and testability of the


application, since all the components can be designed interface-based and tested
using mock objects. Hence, ASP.NET MVC Framework is ideal for projects with large
team of web developers.

 Supports all the existing vast ASP.NET functionalities, such as Authorization and
Authentication, Master Pages, Data Binding, User Controls, Memberships, ASP.NET
Routing, etc.

 Does not use the concept of View State (which is present in ASP.NET). This helps
in building applications, which are lightweight and gives full control to the
developers.

Thus, you can consider MVC Framework as a major framework built on top of ASP.NET
providing a large set of added functionality focusing on component-based development
and testing.

2
MVC Framework – Architecture MVC Framework

In the last chapter, we studied the high-level architecture flow of MVC Framework. Now
let us take a look at how the execution of an MVC application takes place when there is a
certain request from the client. The following diagram illustrates the flow.

MVC Flow Diagram

3
MVC Framework

Flow Steps
Step 1: The client browser sends request to the MVC Application.

Step 2: Global.ascx receives this request and performs routing based on the URL of the
incoming request using the RouteTable, RouteData, UrlRoutingModule and
MvcRouteHandler objects.

Step 3: This routing operation calls the appropriate controller and executes it using the
IControllerFactory object and MvcHandler object's Execute method.

Step 4: The Controller processes the data using Model and invokes the appropriate
method using ControllerActionInvoker object

Step 5: The processed Model is then passed to the View, which in turn renders the final
output.

4
MVC Framework – ASP.NET Forms MVC Framework

MVC and ASP.NET Web Forms are inter-related yet different models of development,
depending on the requirement of the application and other factors. At a high level, you
can consider that MVC is an advanced and sophisticated web application framework
designed with separation of concerns and testability in mind. Both the frameworks have
their advantages and disadvantages depending on specific requirements. This concept can
be visualized using the following diagram:

MVC and ASP.NET Diagram

5
MVC Framework

Comparison Table

6
MVC Framework

End of ebook preview

If you liked what you saw…

Buy it from our store @ https://store.tutorialspoint.com

You might also like