MVC Tutorial
MVC Tutorial
MVC Tutorial
ASP.NET MVC is not something, which is built from ground zero. It is a complete
alternative to traditional ASP.NET Web Forms. It is built on the top of ASP.NET, so
developers enjoy almost all the ASP.NET features while building the MVC application.
Benefits of ASP.NET MVC
• Makes it easier to manage complexity by dividing an application into
the model, the view, and the controller.
• Enables full control over the rendered HTML and provides a clean
separation of concerns.
• Direct control over HTML also means better accessibility for
implementing compliance with evolving Web standards.
Benefits of ASP.NET MVC (Cont.)
• Facilitates adding more interactivity and responsiveness to existing
apps.
• Provides better support for test-driven development (TDD).
• Works well for Web applications that are supported by large teams of
developers and for Web designers who need a high degree of control
over the application behavior.
MVC Pattern
The idea is that you'll have a component called the view, which is solely
responsible for rendering this user interface whether that be HTML or
whether it actually be UI widgets on a desktop application.
The view talks to a model, and that model contains all of the data that the
view needs to display. Views generally don't have much logic inside of them
at all.
Pattern (Cont.)
In a web application, the view might not have any code associated with it at
all. It might just have HTML and then some expressions of where to take pieces
of data from the model and plug them into the correct places inside the HTML
template that you've built in the view.
The controller that organizes is everything. When an HTTP request arrives for
an MVC application, that request gets routed to a controller, and then it's up to
the controller to talk to either the database, the file system, or the model.
Environment Setup
• Open the Visual Studio. Click File → • To keep things simple, select the
New → Project menu option. A ‘Empty’ option and check the MVC
new Project dialog opens. checkbox in the Add folders and
• From the left pane, select core references section. Click Ok.
Templates → Visual C# → Web. • Run this application from Debug →
Start Debugging menu option and you
• In the middle pane, select ASP.NET
will see a 404 Not Found Error.
Web Application.
• Enter the project name,
MVCFirstApp, in the Name field
and click ok to continue.
Routing
• You will define the routes and those routes will map URLs to a specific
controller action. An action is just a method on the controller. It can also pick
parameters out of that URL and pass them as parameters into the method.
• So this route that is defined in the application is the default route. As seen in
the above code, when you see a URL arrive in the form of
(something)/(something)/(something), then the first piece is the controller
name, second piece is the action name, and the third piece is an ID
parameter.
Understanding Routes
• MVC applications use the ASP.NET routing system, which decides how URLs
map to controllers and actions.
• When Visual Studio creates the MVC project, it adds some default routes to
get us started. When you run your application, you will see that Visual Studio
has directed the browser to port 54724. You will almost certainly see a
different port number in the URL that your browser requests because Visual
Studio allocates a random port when the project is created.
Controllers
• Controllers are essentially the central unit of your ASP.NET MVC application.
It is the 1st recipient, which interacts with incoming HTTP Request. So, the
controller decides which model will be selected, and then it takes the data
from the model and passes the same to the respective view, after that view
FileContentResult
2 Returns file content
FilePathResult
3 Returns file content
FileStreamResult
4 Returns file content
5 EmptyResult
Returns nothing
6 JavaScriptResult
Returns script for execution
7 JsonResult
Returns JSON formatted data
8 RedirectToResult
Redirects to the specified URL
9 HttpUnauthorizedResult
Returns 403 HTTP Status code
10 RedirectToRouteResult
Redirects to different action/different controller action
11 ViewResult
Received as a response for view engine
12 PartialViewResult
Received as a response for view engine
Views
• In an ASP.NET MVC application, there is nothing like a page and it also
doesn’t include anything that directly corresponds to a page when you
specify a path in URL. The closest thing to a page in an ASP.NET MVC
application is known as a View.
• A model stores data that is retrieved according to the commands from the
Controller and displayed in the View.
• Model is a collection of classes wherein you will be working with data and
business logic. Hence, basically models are business domain-specific
containers. It is used to interact with database. It can also be used to
manipulate the data to implement the business logic.
Helper
In ASP.Net web forms, developers are using the toolbox for adding controls
on any particular page. However, in ASP.NET MVC application there is no
toolbox available to drag and drop HTML controls on the view. In ASP.NET
MVC application, if you want to create a view it should contain HTML code.
So those developers who are new to MVC especially with web forms
background finds this a little hard.
Helper (Cont.)