Asp .Net MVC
Asp .Net MVC
Asp .Net MVC
NET MVC 5
Nemanja Kojic, MScEE
What is MVC?
Model-View-Controller (MVC)
Standard Architectural Pattern
Separation of concerns:
model, view, controller
2 of 114
Views
Display applications UI
UI created from the model data
Controllers
Handle user input and interaction
Work with model
Select a view for rendering UI
4 of 114
7 of 114
URL routing
ASP .NET routing engine (flexible mapping)
Support for defining customized routing rules
Automatic passing/parsing of parameters
9 of 114
10 of 114
Request handling:
Select appropriate controller
Obtain a specific controller instance
Call the controllers Execute method
11 of 114
Perform routing
Create MVC Request handler
Create controller
Execute controller
Invoke action
Execute result
ViewResult, RedirectToRouteResult, ContentResult,
FileResult, JsonResult, RedirectResult
12 of 114
13 of 114
RAZOR ENGINE
14 of 114
Razor Engine
A new view-engine
Optimized around HTML generation
Code-focused templating approach
15 of 114
16 of 114
Razor HelloWorld
Uses @ for
Razor blocks
razor file
.aspx file
17 of 114
.aspx syntax
18 of 114
Multi-line
statement
Multi-Token
statement
19 of 114
Identifying nested
content with HTML
block tag
20 of 114
Layout/Master page
SiteLayout.cshtml
@RenderBody()
Including specific body content.
21 of 114
Content page
Explicitly setting LayoutPage property.
22 of 114
23 of 114
Named section.
Named section.
24 of 114
25 of 114
26 of 114
27 of 114
28 of 114
Razor Summary
29 of 114
New Project
31
32 of 114
33
34
35
36
ADDING CONTROLLER
37 of 114
Adding controller
38
39
40
41
Mapping controller
Controller selection based on URL
Default URL routing logic:
/[Controller]/[ActionName]/[Parameters]
Format for routing in
App_Start/RouteConfig.cs
42 of 114
URL routing
Webapp URL without URL segments =>
HomeController::Index()
Index() default method of a controller
/HelloWorld => HelloWorldController
/HelloWorld/Index =>
HelloWorldController::Index()
http://webapp:port/HelloWorld/Welcome =>
HelloWorldController::Welcome()
43 of 114
Parameters
/HelloWorld/Welcome?name=Scott&numtimes=4
44 of 114
URL Parameters
http://webapp/HelloWorld/Welcome/3?name=Rick
45 of 114
ADDING A VIEW
46 of 114
Views
47 of 114
48 of 114
Master page.
49 of 114
Index, by default.
50 of 114
ViewBag
Pass data between view template and
layout view file
ViewBag is a dynamic object
(has no defined properties)
View template file.
51 of 114
View
Returns
HelloWorldView
object.
53 of 114
ADDING A MODEL
54 of 114
Model components
Entity framework - data access technology
Code first development paradigm
(first code classes, then generate DB schema)
Database first development paradigm
define db schema first,
then generate models, controllers and views
55 of 114
56 of 114
57 of 114
EF database
context
FETCH,
INSERT,
UPDATE
58 of 114
DB Connection string
59 of 114
60 of 114
Strongly typed
approach.
61 of 114
Run Application
Notice: default routing
62 of 114
63 of 114
64 of 114
65 of 114
Communicates
with the master
page.
Context-sensitive
data access.
66 of 114
67 of 114
Edit View
Localhost:1234/movies/Edit/4
68 of 114
69 of 114
Edit View
70 of 114
Property annotations
Annotations namespace.
71 of 114
ActionLink helper
Html.ActionLink generates a link according to
a given URL mapping policy
Anonymous object
specifies ID of an object
Primer:
Html.ActionLink(Edit", Edit", new{id=item.ID)}
Controller action name.
72 of 114
Edit actions
Implemented as Controllers operations
HTTP GET operation
[Bind] attribute a
security mechanism that
prevents over-posting
data to the model.
[HttpGet] annotation by
default.
73 of 114
74 of 114
ADDING SEARCH
76 of 114
77 of 114
View/Controller changes
View (changes)
Default form
method = POST!
Lambda expression
Use overriden BeginForm method
to force HttpGet method.
78 of 114
HTTP GET
79 of 114
80 of 114
Preselected value.
Parameter movieGenre is
the key for populating
dropdown list from ViewBag.
81 of 114
Populating the
list of genres in
ViewBag.
82 of 114
83 of 114
RULE:
Never use a HttpGet method
to modify the model.
Opens security holes,
architecturally bad!
HttpPost method.
Deletes an object
having the given id.
84 of 114
Data Validation
Keep Things DRY
(Dont Repeat Yourself)
Declarative validation rules in one place
(Model class)
Regular expressions
Range validation
Length validation
NULL values validation
Data formatting
88 of 114
89 of 114
DataType attributes
Provide only hits for the view engine
to format the data
Date, Time, PhoneNumber, EmailAddress,
Automatic provision of type specific features
e.g. mailto: ... link for EmailAddress
Do NOT provide any Validation
(just presentation hints)
90 of 114
DisplayFormat annotation
Used to explicitly specify format of the data
Example: redefining the default date format
91 of 114
LAMBDA EXPRESSIONS
92 of 114
Introduction
Expressions that use special syntax
Anonymous functions used as data
(variables, fields, parameters, return values)
The anonymous functions are used to create
delegates and expression trees
Lambda expressions particularly helpful for
writing LINQ queries
Available from .NET 4.5
93 of 114
Operator =>
Interpreted as goes to
Used for declaring a lambda expression
The same priority as assignment (=)
Right associative operator
Separates the parameters and function body
Left side
=>
Right side
An expression
Anonymous functions
Evolution of delegates in C#
Named method
Inline code
(anonymous method)
Lambda expression
96 of 114
Anonymous method
No name, no overloading
Created using the delegate keyword
It is possible to add multiple statements
inside its body
97 of 114
Expression lambdas
Lambda expression with an expression on the
right side of the operator =>
Used dominantly in construction of
expression trees
(input parameters) => expression
Parentheses optional if lambda has one param.
Input parameters separated by comma
99 of 114
Statement lambdas
(input parameters) => {statement;}
101 of 114
102 of 114
Output: 5, 1, 3, 9, 7
Output: 5, 4, 1, 3
Output: 5, 4
103 of 114
Example:
IEnumerable<Customer> customers=...
Standard query operator.
104 of 114
LINQ
108 of 114
Content
109 of 114
What is LINQ?
LINQ Architecture
111 of 114
LINQ
SQL-Like syntax that deals
with pure objects
Reduces the Impedance
Missmatch
Makes data querying more
efficient
One still must know the
format of the data
112
LINQ Adapters
LINQ to Objects
LINQ to SQL
LINQ to XML
LINQ to Entities
It is possible to create own customized adapter
E.g. LINQ for Querying Twitter API
113 of 114
References
ASP .NET MVC 5 Tutorial Official
http://www.asp.net/mvc/tutorials/mvc5/introduction/getting-started
Lambda expressions
http://www.dotnetperls.com/lambda
LINQ
http://code.msdn.microsoft.com/101-LINQSamples-3fb9811b
114 of 114