Introduction To ASP - NET MVC 4
Introduction To ASP - NET MVC 4
ASP.NET MVC 4
Nikolay Kostov
Senior Software
Developer and
http://nikolay.it
Trainer
Telerik Software
Academy
Table of Contents
The MVC Pattern
ASP.NET MVC
Installation and Creating of
ASP.NET MVC Project
ASP.NET MVC Routing
Controllers and Actions
Razor Views
Areas
Kendo UI Widgets
Model
View
Defines how the applications user
interface (UI) will be displayed
May support master views
(layouts) and sub-views (partial
views or controls)
Web: Template to dynamically
generate HTML
Controller
The core MVC component
Process the requests with the help
of views and models
A set of classes that handles
MVC Steps
HTTP
/
Request
Some/Page
/
Use
r
HTTP Response
View
(render
UI)
Front
controller
(dispatcher)
Delegate
request
Controll
er
Select
view &
pass data
Use model
data
CRUD
model
Model
(data)
9
MVC Frameworks
CakePHP (PHP)
CodeIgniter (PHP)
Spring (Java)
Perl: Catalyst, Dancer
Python: Django, Flask, Grok
Ruby: Ruby on Rails, Camping,
Nitro, Sinatra
JavaScript: AngularJS,
JavaScriptMVC, Spine
ASP.NET MVC (.NET Framework)
10
ASP.NET MVC
ASP.NET Core
ASP.NET
WebFor
ms
ASP.NET
MVC
Presentation
ASP.NET
Caching
.NET
Globalization
Pages
Controls
Master Pages
Profile
Roles
Membership
Routes
Handlers
Etc...
Runtime
12
ASP.NET MVC
15
16
Separation of Concerns
Extensible
18
Clean URLs
REST-like
/products/update
/blog/posts/2013/01/28/mvc-is-cool
Friendlier to humans
/product.aspx?catId=123 or
post.php?id=123
Becomes /products/chocolate/
Web
MVC
Request
Users/Ni
serv
Routing
ki/
er
Select
controller
engine
Controll
and invoke action
(method)
Use
er
r
(C#
HTTP Response
CRUD
Select
view &
(HTML, File,
class)
HTTP
/
JSON, )
View
engine
(Razor)
pass data
(model)
Use model
data
model
Model
(POCO)
20
21
The Technologies
Installation and
Creating of ASP.NET
MVC Project
Demo Project
Features:
User profiles
Forum features
Posting messages
Voting for posts
Administration
Other useful features (tags, search,
24
The Tools
26
tfs.visualstudio.com
Powered by Microsoft
Collaboration platform at the core
of application lifecycle
management (ALM)
Source control system (TFS)
Free plan that includes:
Version control
Free builds
Up to 5 users
Unlimited number of projects
27
28
Internet Application
Project
29
Demo: Internet
application
Making changes and debugging
NuGet package
management
32
Demo:
NuGet
Install and update
packages as easy as
adding a reference
ASP.NET MVC
Routing
System.Web.Routing.RouteTable.Ro
utes
Register routes
In Global.asax in the
Application_Start() there is
RouteConfig.RegisterRoutes(RouteTable.
Routes);
Default
parameters
36
Routing Examples
http://localhost/Products
/ById/3
Controller: Products
Action: ById
Id: 3
37
http://localhost/Products
/ById
Controller: Products
Action: ById
Id: 0 (optional parameter)
38
http://localhost/Products
Controller: Products
Action: Index
Id: 0 (optional parameter)
39
http://localhost/
Controller: Home
Action: Index
Id: 0 (optional parameter)
40
Custom Route
http://localhost/Users/Nik
olayIT
Controller: Users
Action: ByUsername
Username: NikolayIT
41
http://localhost/Users
Controller: Users
Action: ByUsername
Username: DefaultValue
42
?
http://localhost/Users
43
Route Constraints
Constraints are rules on the URL
segments
All the constraints are regular
expression compatible with class
Regex
Defined as one of the
routes.MapRoute() parameters
44
Debugging Routes
Demo: Routes
ASP.NET MVC Routing
Controllers and
Actions
The brain
of the application
Controllers
The core component of the MVC
pattern
All the controllers should be
available in a folder by name
Controllers
Controller naming standard should
be "nameController" (convention)
Routers instantiate controllers in
every request
Actions
49
Action Results
Controller action response to a
browser request
Inherits from the base ActionResult
class
Different results types:
Name
Framework Behavior
Producing
Method
ContentResult
Content
EmptyResult
No response
FileContentResult
FilePathResult
FileStreamResult
File
50
Framework
Behavior
Producing
Method
Returns a script to
execute
JavaScript
JsonResult
Json
RedirectResult
Redirect /
RedirectPermane
nt
RedirectToRouteRes
ult
Redirect to another
action, or another
controllers action
RedirectToRoute /
RedirectToAction
ViewResult
PartialViewResult
Response is the
View /
responsibility of a view PartialView
engine
51
Action Parameters
52
Action Selectors
ActionName(string name)
AcceptVerbs
HttpPost
HttpGet
HttpDelete
HttpOptions
NonAction
RequireHttps
ChildActionOnly Only for
Html.Action()
53
Action Filters
Apply pre- and post-processing
logic
Can be applied to actions and to
controllers
Global filters can be registered in
Name
Description
GlobalFilters.
Filters
(orofin
OutputCache
Cache the output
a controller
ValidateInput(false)
Turn off request validation and allow
/App_Start/FilterConfig.cs)
Authorize
ValidateAntiForgeryToken
HandleError
54
OnActionExecuting(ActionExecuting
Context)
OnActionExecuted(ActionExecutedC
ontext)
OnResultExecuting(ResultExecuting
Context)
OnResultExecuted(ResultExecutedC
ontext)
55
Razor Views
Views
HTML templates of the application
A lot of view engines available
58
Razor
Template markup syntax
Simple-syntax view engine
Based on the C# programming
language
Enables the programmer to use an
HTML construction workflow
Code-focused templating approach,
with minimal transition between
HTML and code
59
Design Goals
Template
Easy to Learn
Is not a new language
Works with any Text Editor
Data
Has great Intellisense
Unit Testable
Generate
d Output
60
Strongly-typed views:
Action: return View(model);
View: @model ModelDataType;
61
How it works?
ByUsername.cs
html
Template
HTML
Output
Data
Generated
Output
UsersControll
er.cs
UserModel
.cs
62
Razor Syntax
63
Comments
@*
A Razor Comment
*@
@{
//A C# comment
/* A Multi
line C# comment
*/
@* 6 / 10.0 *@
@* 0.6 *@
@*
@* spam_me6 *@
@model UserModel
<p>@Model.Username</p>
66
Layout
Define a common site template
Similar to ASP.NET master pages
(but better!)
Razor view engine renders content
inside-out
@RenderBody()
indicate where we want
the views based on this
layout to fill in their
core content at that
67
@{
Layout = "~/Views/Shared/_UncommonLayout.cshtml";
}
Sections
You can have one or more
"sections" (optional)
They are defined in the views:
69
View Helpers
70
HTML Helpers
Method
Type
Description
BeginForm,
BeginRouteForm
Form
EndForm
Form
CheckBox,
CheckBoxFor
Input
Hidden, HiddenFor
Input
Password,
PasswordFor
Input
RadioButton,
RadioButtonFor
Input
TextBox,
TextBoxFor
Input
Label, LabelFor
Label
71
Type
Description
ActionLink,
RouteLink
Link
DropDownList,
DropDownListFor
List
ListBox,
ListBoxFor
List
TextArea,
TextAreaFor
TextArea
Partial
Partial
RenderPartial
Partial
ValidationMessag
e,
ValidationMessag
Custom Helpers
73
74
Partial Views
request
75
Areas
Areas
Some applications can have a large
number of controllers
ASP.NET MVC lets us partition Web
applications into smaller units
(areas)
An area is effectively an MVC
structure inside an application
Example: large e-commerce
application
77
Demo: Areas
Kendo UI Widgets
http://www.kendoui.com/
JavaScript
HTML5
CSS3
jQuery
80
Rich UI Widgets
Client-side DataSource
Abstraction for working with all
types of data on the client side
81
MVVM Framework
ASP.NET MVC
Java and PHP
82
Include Kendo UI in
Project
83
Include Kendo UI in
Project (2)
In Views\Shared\_Layout.cshtml before
</body>
Add namespaces in
~/Views/Web.config
84
Using Kendo UI
Demo: Kendo UI
http://
demos.kendoui.com/web/overview/index.h
tml
Summary
Modelviewcontroller (MVC) is a
software architecture pattern
ASP.NET MVC is a great platform
for developing internet
applications
Routes maps URLs to controllers
and actions
Razor views are powerful engine
for combining models and
templates into HTML code
Our project can be divided into
87
Introduction to ASP.NET
MVC 4
?
?
?
,
SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "
Questions?
?
http
C# Programming @ Telerik
Academy
academy.telerik.com
csharpfundamentals.telerik.com
facebook.com/TelerikAcademy
forums.academy.telerik.com