SlideShare a Scribd company logo
ASP.NET MVC
MVC
• MVC separates the user interface of an
application into three main aspects:
– The Model
– The View
– The Controler
MVC
• Model
A set of classes that describes the data you’re working with as
well as the business rules for how the data can be changed
and manipulated
• View
Defines how the application’s UI will be displayed
• Controller
A set of classes that handles communication from the user,
overall application flow, and application-specific logic.
MVC as Applied to Web Frameworks
• Models:
– Represent domain objects that encapsulate:
• data stored in a database
• code to manipulate the data
• code to enforce domain-specific business logic
• Example: encapsulating Entity Framework
MVC as Applied to Web Frameworks
• View:
– Template to dynamically generate HTML
• Controller:
– Class that manages the relationship between the
View and the Model
– It responds to user input, talks to the model, and
decides which view to render (if any).
Asp 1-mvc introduction
A Pattern
• MVC is a pattern that can be applied to
different frameworks
• ASP.NET MVC
– The MVC pattern has been applied to ASP.NET.
This does not mean that it is the same as MVC
running in other places.
ASP.NET MVC 3
• 10 months after MVC 2
– The Razor view engine
– Support for .NET 4 Data Annotations
– Improved model validation
– Greater control and flexibility with support for
dependency resolution and global action filters
– Better JavaScript support with unobtrusive
JavaScript, jQuery Validation, and JSON binding
– Use of NuGet to deliver software and manage
dependencies throughout the platform
Razor View Engine
• Code-focused templating for HTML generation
• Compact, expressive, and fluid
• Not a new language
• Easy to learn
• Works with any text editor
• IntelliSense
• No XML-like heavy syntax
ASP.NET MVC 4
• Features include:
– ASP.NET Web API
– Enhancements to the default project templates
– Mobile project template using jQuery Mobile
– Display Modes
– Task Support for Asynchronous Controllers
– Bundling and Minification
ASP.NET Web API
• Referred to as Web API
• A framework that offers the ASP.NET MVC
development style but is tailored to writing HTTP
services. (service-oriented design)
• Several MVC features have been adopted for
HTTP Service domain:
– Routing
– Model Binding and Validation
– Filters
– Scaffolding
– Easy Unit Testability
ASP.NET MVC 5
• http://www.asp.net/mvc/mvc5
Layout of an MVC project
When you create a new MVC project, your solution
should have the following structure in your Solution
Explorer.
13
Layout of an MVC project
14
Layout of an MVC project
15
• App_Data –it’s meant to hold data for your application (just as the
name says). A couple of examples would include a portable database
(like SQL Server Compact Edition) or any kind of data files (XML, JSON,
etc.). I prefer to use SQL Server.
• App_Start – The App_Start folder contains the initialization and
configuration of different features of your application.
– BundleConfig.cs – This contains all of the configuration for minifying and
compressing your JavaScript and CSS files into one file.
– FilterConfig.cs – Registers Global Filters.
– RouteConfig.cs – Configuration of your routes.
There are other xxxxConfig.cs files that are added when you apply other MVC-
related technologies (for example, WebAPI adds WebApiConfig.cs).
• Content – This folder is meant for all of your static content like images
and style sheets. It’s best to create folders for them like “images” or
“styles” or “css”.
Layout of an MVC project
16
• Controllers – The controllers folder is where we place the
controllers.
• Models – This folder contains your business models. It’s better
when you have these models in another project, but for demo
purposes, we’ll place them in here.
• Scripts – This is where your JavaScript scripts reside.
• Views – This parent folder contains all of your HTML “Views”
with each controller name as a folder. Each folder will contain a
number of cshtml files relating to the methods in that folder’s
controller. If we had a URL that looked like this:
http://www.xyzcompany.com/Products/List
we would have a Products folder with a List.cshtml file.
We would also know to look in the Controllers folder and
open the ProductsController.cs and look for the List
method.
Layout of an MVC project
17
• Views/Shared – The Shared folder is meant for any shared
cshtml files you need across the website.
• Global.asax – The Global.asax is meant for the initialization
of the web application. If you look inside the Global.asax,
you’ll notice that this is where the RouteConfig.cs,
BundleConfig.cs, and FilterConfig.cs are called when the
application runs.
• Web.Config – The web.config is where you place
configuration settings for your application. For new MVC
developers, it’s good to know that you can place settings
inside the <appsettings> tag and place connection strings
inside the <connectionstring> tag.
Controller
• “Convention over configuration”
• ASP.NET MVC is heavily convention-based
– Uses Directory-naming structure when resolving
view templates.
• Allows you to omit the location path when referencing
views from within a Controller class.
View
• Each controller’s class name ends with Controller
– LegalController, LegalServicesController, HomeController
• One Views directory for all the views of your
application
• Views that controllers use live in a subdirectory of the
Views main directory and are named according to the
controller name(minus the Controller suffix)
– Views for the LegalController would live in /Views/Legal
• Reusable UI elements live in a similar structure, but in
a Shared directory in the Views folder
Controller Basics
• Model View Controller:
– 1st Controller basics, then the other stuff.
– Have a look at the HomeController
• Responsible for deciding what will happen when you
browse to the homepage of the website.
The Controller’s Role
• Responsible for responding to user input,
making changes to the model in response to
user input if needed.
• Controllers are concerned with the flow of
the application, working with data that comes
in and providing data going out to the relevant
view.
The Controller’s Role
• The URL tells the routing mechanism (later
chapter) which controller class to instantiate
and which action method to call, and supplies
the required arguments to that method.
• The controller’s method then decides which
view to use, and that view then renders the
HTML.
• The controller’s method could return
something other than a view.
The Controller’s Role
• There is a relationship between the URL and
the METHOD on a controller class. Not a
relationship between the URL and a FILE on
the web server.
• MVC serves up the results of method calls, not
dynamically generated pages.
• (More about routing in later)
MVC Request processing pipeline
Modifying the LegalServicesController
• The Browse and Details methods to cover additional
scenarios.
Observation
• Browsing to /LegalServices/Browse caused the
browse method of the LegalServicesController
to be executed.
– NO extra configuration was needed.
– This is routing in action. More later.
• We returned text to the browser without a
model or a view.
Oversimplified
• The previous example was not common and
was oversimplified.
– You will almost always use Views
– You will most often return ActionResult instead of
strings
– And routing is more complex that shown.
Mapping controller
• Controller selection based on URL
• Default URL routing
logic:/[Controller]/[ActionName]/[Parameters]
• Format for routing
inApp_Start/RouteConfig.cs
URL routing
• WebappURL without URL segments =>
HomeController::Index()
• Index() –default method of a controller
• /HelloWorld=> HelloWorldController
• /HelloWorld/Index =>
HelloWorldController::Index()
• http://webapp:port/HelloWorld/Welcome
=>HelloWorldController::Welcome()
Parameters
• /HelloWorld/Welcome?name=Scott&numtimes=4
• Introducing 2 parameters to Welcome method
• Parameters passed as query strings!
URL Parameters
• http://webapp/HelloWorld/Welcome/3?name
=Rick
Basic Controllers Summary
• Controllers orchestrate the interactions of the
user, the model objects and the views.
• They are responsible for:
– responding to the user input
– Manipulating the appropriate model objects
– And then selecting the appropriate view to display
back to the user in response to the initial input
ActionResult
• Before talking about Views, let’s look at
ActionResults
ActionResult type
The View
• The user’s first impression of your site starts
with the View
• Responsible for providing the user interface to
the user.
• The view transforms a model into a format
ready to be presented to the user.
• The view examines the model object and
transforms the contents to HTML
The View
• Not all views render HTML, but HTML is the
most common case
• More info on alternate types of content later.
• Views created using Razor view engine
• Controller method returns View object
• Controller method return type is ActionResult
Razor View Engine
• What is Razor?
– Introduced in ASP.NET MVC 3 and is the default
view engine moving forward
– Provides a clean, lightweight, simple view engine.
– Provides a streamlined syntax for expressing views
– Minimizes the amount of syntax and extra
characters.
Example
Convention
• The Views directory contains a folder for your
controller, with the same name as the controller,
but without the controller suffix.
• HomeController has views in the Home directory
• Within the view folder (also called controller
folder) there’s a view file for each action method,
named the same as the action method.
• This is how views are associated to an action
method.
• An action method can return a ViewResult via
the View() method.
View() Method
• When the view name isn’t specified, the ViewResult
returned by the action method applies a convention to
locate the view.
– It first looks for a view with the same name as the action within
the /Views/ControllerName directory. “Ex:
/views/home/Index.cshtml”
• The View() method is overloaded. You can supply a view
name to render a different view. “ex: View(“MyView”)”
– It will still look in the same directory, but will look for the
“MyView.cshtml” file.
• View(“~/Views/DifferentDirectory/Index.cshtml”)
– To get to a different directory, provide the full path to that
directory. You must provide the file extension as well. This
bypasses the internal lookup mechanism.

More Related Content

PPTX
asp-net.pptx
PPTX
Using MVC with Kentico 8
PPTX
Using the Kentico CMS API
PDF
MVC Web Application
PPT
Asp.net,mvc
PDF
Dot net interview questions and asnwers
PPTX
Asp.net With mvc handson
PPTX
ASP .NET MVC
asp-net.pptx
Using MVC with Kentico 8
Using the Kentico CMS API
MVC Web Application
Asp.net,mvc
Dot net interview questions and asnwers
Asp.net With mvc handson
ASP .NET MVC

What's hot (20)

PPTX
Word press 01
PPTX
Asp.net mvc presentation by Nitin Sawant
PPSX
Asp.net mvc
PPTX
Asp.net mvc 5 course module 1 overview
PPT
Introduction to ASP.NET MVC 1.0
PDF
Asp.net mvc basic introduction
PDF
MVC with Zend Framework
PPTX
Mvc framework
PDF
Building Web Applications with Zend Framework
PPT
ASP.NET MVC Presentation
PPTX
Getting started with MVC 5 and Visual Studio 2013
PPTX
Introduction to ASP.NET MVC
PPTX
Introduction to ASP.Net MVC
PDF
Mastering asp.net mvc - Dot Net Tricks
PPTX
Sitecore MVC (User Group Conference, May 23rd 2014)
PDF
Asp 1a-aspnetmvc
PPTX
MVC 6 Introduction
PPTX
Sitecore MVC: Converting Web Forms sublayouts
PPTX
Learning ASP.NET 5 and MVC 6
PPTX
Asp.net MVC training session
Word press 01
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc
Asp.net mvc 5 course module 1 overview
Introduction to ASP.NET MVC 1.0
Asp.net mvc basic introduction
MVC with Zend Framework
Mvc framework
Building Web Applications with Zend Framework
ASP.NET MVC Presentation
Getting started with MVC 5 and Visual Studio 2013
Introduction to ASP.NET MVC
Introduction to ASP.Net MVC
Mastering asp.net mvc - Dot Net Tricks
Sitecore MVC (User Group Conference, May 23rd 2014)
Asp 1a-aspnetmvc
MVC 6 Introduction
Sitecore MVC: Converting Web Forms sublayouts
Learning ASP.NET 5 and MVC 6
Asp.net MVC training session
Ad

Similar to Asp 1-mvc introduction (20)

PDF
Aspnetmvc 1
PPTX
PPTX
MVC Framework
PPTX
PPTX
Architectural Design & Patterns
PPTX
PPTX
E-Commerce Applications Development
PPT
Mvc 130330091359-phpapp01
PPTX
Asp.net c# MVC-5 Training-Day-1 of Day-9
PPTX
Sitecore mvc
PPTX
Session 1
PPTX
ASP.NET - Building Web Application..in the right way!
PPTX
ASP.NET - Building Web Application..in the right way!
PPT
Web engineering - MVC
PPTX
Spring Web Presentation 123143242341234234
PDF
Lecture 05 - Creating a website with Razor Pages.pdf
PPTX
4. Introduction to ASP.NET MVC - Part I
PPTX
Simple mvc4 prepared by gigin krishnan
PPT
Asp.net mvc
Aspnetmvc 1
MVC Framework
Architectural Design & Patterns
E-Commerce Applications Development
Mvc 130330091359-phpapp01
Asp.net c# MVC-5 Training-Day-1 of Day-9
Sitecore mvc
Session 1
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
Web engineering - MVC
Spring Web Presentation 123143242341234234
Lecture 05 - Creating a website with Razor Pages.pdf
4. Introduction to ASP.NET MVC - Part I
Simple mvc4 prepared by gigin krishnan
Asp.net mvc
Ad

More from Fajar Baskoro (20)

PPTX
Pengembangan Basis Data untuk Web Application.pptx
PPTX
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
PPTX
Sosialisasi Program Digital Skills Unicef 2025.pptx
PDF
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
PDF
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
PDF
Pemrograman Mobile menggunakan kotlin2.pdf
PPTX
Membangun Kewirausahan Sosial Program Double Track.pptx
PPTX
Membangun Kemandirian DTMandiri-2025.pptx
PDF
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
PDF
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
PPTX
Seleksi Penerimaan Murid Baru 2025.pptx
PPTX
Pengembangan Program Dual Track 2025-2.pptx
PPTX
Pengembangan Program Dual Track 2025-1.pptx
PDF
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PPTX
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PPTX
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PPTX
Program Dual Track Kalimantan Timur 2025.pptx
PDF
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
PPTX
Pengembangan Program Digital Skills - 2025.pptx
PPTX
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Pengembangan Basis Data untuk Web Application.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Seleksi Penerimaan Murid Baru 2025.pptx
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-1.pptx
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Pengembangan Program Digital Skills - 2025.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Trump Administration's workforce development strategy
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Classroom Observation Tools for Teachers
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Trump Administration's workforce development strategy
Weekly quiz Compilation Jan -July 25.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
O7-L3 Supply Chain Operations - ICLT Program
Classroom Observation Tools for Teachers
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
A systematic review of self-coping strategies used by university students to ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pharma ospi slides which help in ospi learning
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life

Asp 1-mvc introduction

  • 2. MVC • MVC separates the user interface of an application into three main aspects: – The Model – The View – The Controler
  • 3. MVC • Model A set of classes that describes the data you’re working with as well as the business rules for how the data can be changed and manipulated • View Defines how the application’s UI will be displayed • Controller A set of classes that handles communication from the user, overall application flow, and application-specific logic.
  • 4. MVC as Applied to Web Frameworks • Models: – Represent domain objects that encapsulate: • data stored in a database • code to manipulate the data • code to enforce domain-specific business logic • Example: encapsulating Entity Framework
  • 5. MVC as Applied to Web Frameworks • View: – Template to dynamically generate HTML • Controller: – Class that manages the relationship between the View and the Model – It responds to user input, talks to the model, and decides which view to render (if any).
  • 7. A Pattern • MVC is a pattern that can be applied to different frameworks • ASP.NET MVC – The MVC pattern has been applied to ASP.NET. This does not mean that it is the same as MVC running in other places.
  • 8. ASP.NET MVC 3 • 10 months after MVC 2 – The Razor view engine – Support for .NET 4 Data Annotations – Improved model validation – Greater control and flexibility with support for dependency resolution and global action filters – Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding – Use of NuGet to deliver software and manage dependencies throughout the platform
  • 9. Razor View Engine • Code-focused templating for HTML generation • Compact, expressive, and fluid • Not a new language • Easy to learn • Works with any text editor • IntelliSense • No XML-like heavy syntax
  • 10. ASP.NET MVC 4 • Features include: – ASP.NET Web API – Enhancements to the default project templates – Mobile project template using jQuery Mobile – Display Modes – Task Support for Asynchronous Controllers – Bundling and Minification
  • 11. ASP.NET Web API • Referred to as Web API • A framework that offers the ASP.NET MVC development style but is tailored to writing HTTP services. (service-oriented design) • Several MVC features have been adopted for HTTP Service domain: – Routing – Model Binding and Validation – Filters – Scaffolding – Easy Unit Testability
  • 12. ASP.NET MVC 5 • http://www.asp.net/mvc/mvc5
  • 13. Layout of an MVC project When you create a new MVC project, your solution should have the following structure in your Solution Explorer. 13
  • 14. Layout of an MVC project 14
  • 15. Layout of an MVC project 15 • App_Data –it’s meant to hold data for your application (just as the name says). A couple of examples would include a portable database (like SQL Server Compact Edition) or any kind of data files (XML, JSON, etc.). I prefer to use SQL Server. • App_Start – The App_Start folder contains the initialization and configuration of different features of your application. – BundleConfig.cs – This contains all of the configuration for minifying and compressing your JavaScript and CSS files into one file. – FilterConfig.cs – Registers Global Filters. – RouteConfig.cs – Configuration of your routes. There are other xxxxConfig.cs files that are added when you apply other MVC- related technologies (for example, WebAPI adds WebApiConfig.cs). • Content – This folder is meant for all of your static content like images and style sheets. It’s best to create folders for them like “images” or “styles” or “css”.
  • 16. Layout of an MVC project 16 • Controllers – The controllers folder is where we place the controllers. • Models – This folder contains your business models. It’s better when you have these models in another project, but for demo purposes, we’ll place them in here. • Scripts – This is where your JavaScript scripts reside. • Views – This parent folder contains all of your HTML “Views” with each controller name as a folder. Each folder will contain a number of cshtml files relating to the methods in that folder’s controller. If we had a URL that looked like this: http://www.xyzcompany.com/Products/List we would have a Products folder with a List.cshtml file. We would also know to look in the Controllers folder and open the ProductsController.cs and look for the List method.
  • 17. Layout of an MVC project 17 • Views/Shared – The Shared folder is meant for any shared cshtml files you need across the website. • Global.asax – The Global.asax is meant for the initialization of the web application. If you look inside the Global.asax, you’ll notice that this is where the RouteConfig.cs, BundleConfig.cs, and FilterConfig.cs are called when the application runs. • Web.Config – The web.config is where you place configuration settings for your application. For new MVC developers, it’s good to know that you can place settings inside the <appsettings> tag and place connection strings inside the <connectionstring> tag.
  • 18. Controller • “Convention over configuration” • ASP.NET MVC is heavily convention-based – Uses Directory-naming structure when resolving view templates. • Allows you to omit the location path when referencing views from within a Controller class.
  • 19. View • Each controller’s class name ends with Controller – LegalController, LegalServicesController, HomeController • One Views directory for all the views of your application • Views that controllers use live in a subdirectory of the Views main directory and are named according to the controller name(minus the Controller suffix) – Views for the LegalController would live in /Views/Legal • Reusable UI elements live in a similar structure, but in a Shared directory in the Views folder
  • 20. Controller Basics • Model View Controller: – 1st Controller basics, then the other stuff. – Have a look at the HomeController • Responsible for deciding what will happen when you browse to the homepage of the website.
  • 21. The Controller’s Role • Responsible for responding to user input, making changes to the model in response to user input if needed. • Controllers are concerned with the flow of the application, working with data that comes in and providing data going out to the relevant view.
  • 22. The Controller’s Role • The URL tells the routing mechanism (later chapter) which controller class to instantiate and which action method to call, and supplies the required arguments to that method. • The controller’s method then decides which view to use, and that view then renders the HTML. • The controller’s method could return something other than a view.
  • 23. The Controller’s Role • There is a relationship between the URL and the METHOD on a controller class. Not a relationship between the URL and a FILE on the web server. • MVC serves up the results of method calls, not dynamically generated pages. • (More about routing in later)
  • 25. Modifying the LegalServicesController • The Browse and Details methods to cover additional scenarios.
  • 26. Observation • Browsing to /LegalServices/Browse caused the browse method of the LegalServicesController to be executed. – NO extra configuration was needed. – This is routing in action. More later. • We returned text to the browser without a model or a view.
  • 27. Oversimplified • The previous example was not common and was oversimplified. – You will almost always use Views – You will most often return ActionResult instead of strings – And routing is more complex that shown.
  • 28. Mapping controller • Controller selection based on URL • Default URL routing logic:/[Controller]/[ActionName]/[Parameters] • Format for routing inApp_Start/RouteConfig.cs
  • 29. URL routing • WebappURL without URL segments => HomeController::Index() • Index() –default method of a controller • /HelloWorld=> HelloWorldController • /HelloWorld/Index => HelloWorldController::Index() • http://webapp:port/HelloWorld/Welcome =>HelloWorldController::Welcome()
  • 30. Parameters • /HelloWorld/Welcome?name=Scott&numtimes=4 • Introducing 2 parameters to Welcome method • Parameters passed as query strings!
  • 32. Basic Controllers Summary • Controllers orchestrate the interactions of the user, the model objects and the views. • They are responsible for: – responding to the user input – Manipulating the appropriate model objects – And then selecting the appropriate view to display back to the user in response to the initial input
  • 33. ActionResult • Before talking about Views, let’s look at ActionResults
  • 35. The View • The user’s first impression of your site starts with the View • Responsible for providing the user interface to the user. • The view transforms a model into a format ready to be presented to the user. • The view examines the model object and transforms the contents to HTML
  • 36. The View • Not all views render HTML, but HTML is the most common case • More info on alternate types of content later. • Views created using Razor view engine • Controller method returns View object • Controller method return type is ActionResult
  • 37. Razor View Engine • What is Razor? – Introduced in ASP.NET MVC 3 and is the default view engine moving forward – Provides a clean, lightweight, simple view engine. – Provides a streamlined syntax for expressing views – Minimizes the amount of syntax and extra characters.
  • 39. Convention • The Views directory contains a folder for your controller, with the same name as the controller, but without the controller suffix. • HomeController has views in the Home directory • Within the view folder (also called controller folder) there’s a view file for each action method, named the same as the action method. • This is how views are associated to an action method. • An action method can return a ViewResult via the View() method.
  • 40. View() Method • When the view name isn’t specified, the ViewResult returned by the action method applies a convention to locate the view. – It first looks for a view with the same name as the action within the /Views/ControllerName directory. “Ex: /views/home/Index.cshtml” • The View() method is overloaded. You can supply a view name to render a different view. “ex: View(“MyView”)” – It will still look in the same directory, but will look for the “MyView.cshtml” file. • View(“~/Views/DifferentDirectory/Index.cshtml”) – To get to a different directory, provide the full path to that directory. You must provide the file extension as well. This bypasses the internal lookup mechanism.