SlideShare a Scribd company logo
1 of 40
Download to read offline
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).
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

What's hot

Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantNitin Sawant
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overviewSergey Seletsky
 
Introduction to ASP.NET MVC 1.0
Introduction to ASP.NET MVC 1.0Introduction to ASP.NET MVC 1.0
Introduction to ASP.NET MVC 1.0Shiju Varghese
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introductionBhagath Gopinath
 
MVC with Zend Framework
MVC with Zend FrameworkMVC with Zend Framework
MVC with Zend Frameworkwebholics
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend FrameworkPhil Brown
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentationivpol
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Thomas Robbins
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVCEmad Alashi
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVCSagar Kamate
 
Mastering asp.net mvc - Dot Net Tricks
Mastering asp.net mvc - Dot Net TricksMastering asp.net mvc - Dot Net Tricks
Mastering asp.net mvc - Dot Net TricksGaurav Singh
 
Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Ruud van Falier
 
Sitecore MVC: Converting Web Forms sublayouts
Sitecore MVC: Converting Web Forms sublayoutsSitecore MVC: Converting Web Forms sublayouts
Sitecore MVC: Converting Web Forms sublayoutsnonlinear creations
 
Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Ido Flatow
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training sessionHrichi Mohamed
 

What's hot (20)

Word press 01
Word press 01Word press 01
Word press 01
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
 
Introduction to ASP.NET MVC 1.0
Introduction to ASP.NET MVC 1.0Introduction to ASP.NET MVC 1.0
Introduction to ASP.NET MVC 1.0
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
 
MVC with Zend Framework
MVC with Zend FrameworkMVC with Zend Framework
MVC with Zend Framework
 
Mvc framework
Mvc frameworkMvc framework
Mvc framework
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend Framework
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVC
 
Mastering asp.net mvc - Dot Net Tricks
Mastering asp.net mvc - Dot Net TricksMastering asp.net mvc - Dot Net Tricks
Mastering asp.net mvc - Dot Net Tricks
 
Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
 
Sitecore MVC: Converting Web Forms sublayouts
Sitecore MVC: Converting Web Forms sublayoutsSitecore MVC: Converting Web Forms sublayouts
Sitecore MVC: Converting Web Forms sublayouts
 
Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training session
 

Similar to ASP.NET MVC Framework Guide

Similar to ASP.NET MVC Framework Guide (20)

Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
Mvc
MvcMvc
Mvc
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
MVC 4
MVC 4MVC 4
MVC 4
 
Architectural Design & Patterns
Architectural Design&PatternsArchitectural Design&Patterns
Architectural Design & Patterns
 
Mvc4
Mvc4Mvc4
Mvc4
 
MVC architecture
MVC architectureMVC architecture
MVC architecture
 
E-Commerce Applications Development
E-Commerce Applications Development E-Commerce Applications Development
E-Commerce Applications Development
 
Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01
 
Asp.net c# MVC-5 Training-Day-1 of Day-9
Asp.net c# MVC-5 Training-Day-1 of Day-9Asp.net c# MVC-5 Training-Day-1 of Day-9
Asp.net c# MVC-5 Training-Day-1 of Day-9
 
Sitecore mvc
Sitecore mvcSitecore mvc
Sitecore mvc
 
Session 1
Session 1Session 1
Session 1
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
 
Web engineering - MVC
Web engineering - MVCWeb engineering - MVC
Web engineering - MVC
 
Lecture 05 - Creating a website with Razor Pages.pdf
Lecture 05 - Creating a website with Razor Pages.pdfLecture 05 - Creating a website with Razor Pages.pdf
Lecture 05 - Creating a website with Razor Pages.pdf
 
4. Introduction to ASP.NET MVC - Part I
4. Introduction to ASP.NET MVC - Part I4. Introduction to ASP.NET MVC - Part I
4. Introduction to ASP.NET MVC - Part I
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnan
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 

More from Fajar Baskoro

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxFajar Baskoro
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterFajar Baskoro
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanFajar Baskoro
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUSFajar Baskoro
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdfFajar Baskoro
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptxFajar Baskoro
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxFajar Baskoro
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimFajar Baskoro
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahFajar Baskoro
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaFajar Baskoro
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetFajar Baskoro
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdfFajar Baskoro
 

More from Fajar Baskoro (20)

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptx
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarter
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUS
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptx
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptx
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptx
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi Kaltim
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolah
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remaja
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
 
epl1.pdf
epl1.pdfepl1.pdf
epl1.pdf
 
user.docx
user.docxuser.docx
user.docx
 
Dtmart.pptx
Dtmart.pptxDtmart.pptx
Dtmart.pptx
 
DualTrack-2023.pptx
DualTrack-2023.pptxDualTrack-2023.pptx
DualTrack-2023.pptx
 
BADGE.pptx
BADGE.pptxBADGE.pptx
BADGE.pptx
 
womenatwork.pdf
womenatwork.pdfwomenatwork.pdf
womenatwork.pdf
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdf
 

Recently uploaded

Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

ASP.NET MVC Framework Guide

  • 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).
  • 6.
  • 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.