Introduction To ASP - NET MVC 4
Introduction To ASP - NET MVC 4
ASP.NET MVC 4
Nikolay Kostov
Senior Software
Developer and Trainer
http://nikolay.it
Telerik Software Academy
Table of Contents
The MVC Pattern
ASP.NET MVC
Areas
Kendo UI Widgets
2
The MVC Pattern
The MVC Pattern
Model–view–controller (MVC) is a software
architecture pattern
Originally
formulated in the late 1970s by
Trygve Reenskaug as part of the Smalltalk
Code reusability and separation of concerns
Originallydeveloped for
desktop, then adapted
for internet applications
4
Model
Set of classes
that describes the data we are
working with as well as the business
Rules
for how the data can be
changed and manipulated
May contain data validation rules
Often encapsulate data stored in a database as
well as code used to manipulate the data
Most likely a Data Access Layer of some kind
6
Controller
The core MVC component
7
MVC Steps
Incoming request routed to Controller
9
MVC Frameworks
CakePHP (PHP)
CodeIgniter (PHP)
Spring (Java)
10
ASP.NET MVC
ASP.NET Core
Presentation
ASP.NET
Caching .NET Globalization
12
ASP.NET Web Forms
Stable and mature, supported by heaps of
third party controls and tools
Event driven web development
Postbacks
Viewstate
Hard to test
Rapid development
ASP.NET MVC
Runs on top of ASP.NET
Testable
/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/
Friendlier to web crawlers
19
MVC Pattern in ASP.NET MVC
20
ASP.NET MVC Request
21
The Technologies
Technologies that ASP.NET MVC uses
22
Installation and Creating of
ASP.NET MVC Project
Demo Project
Forum system like http://stackoverflow.com
StackOverflow Forum Internet Application
Features:
User profiles
Forum features
Posting messages
Voting for posts
Administration
Other useful features (tags, search, comments)
24
The Tools
Tools that we need:
IDE: Visual Studio 2012 (Express for Web)
JustCode and Web Essentals
Framework: .NET Framework 4.5
Web server: IIS 8 (Express)
Data: Microsoft SQL Sever (Express or LocalDB)
Web PlatformInstaller 4.0 will install
everything we need for us
microsoft.com/web/downloads/platform.aspx
Install Visual Studio Express 2012 for Web
Web Platform Installer
26
tfs.visualstudio.com
Powered by Microsoft
Version control
Free builds
Up to 5 users
Unlimited number of projects
27
New ASP.NET MVC Project
28
Internet Application Project
29
Internet App Project Files
Static files (CSS, Images, etc.)
View templates
Configuration file
30
Demo: Internet application
Making changes and debugging
NuGet package management
Free, open source package management
Makes it easy to install
and update open
source libraries and tools
Part of Visual Studio 2012
32
Demo: NuGet
Install and update packages as easy
as adding a reference
ASP.NET MVC Routing
ASP.NET MVC Routing
Mapping between patterns and a combination
of controller + action + parameters
Routes are defined as a global list of routes
System.Web.Routing.RouteTable.Routes
Something similar to Apache mod_rewrite
Greedy algorithm
Route name
Route pattern
Default parameters
36
Routing Examples
http://localhost/Products/ById/3
Controller: Products
Action: ById
Id: 3
37
Routing Examples (2)
http://localhost/Products/ById
Controller: Products
Action: ById
Id: 0 (optional parameter)
38
Routing Examples (3)
http://localhost/Products
Controller: Products
Action: Index
http://localhost/
Controller: Home
Action: Index
http://localhost/Users/NikolayIT
Controller: Users
Action: ByUsername
Username: NikolayIT
41
Custom Route (2)
http://localhost/Users
Controller: Users
Action: ByUsername
Username: DefaultValue
42
Custom Route (3)
?
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
In actions
we have access to a data structure
called RouteData
RouteData.Values["controller"]
RouteData.Values["action"]
RouteData.Values["id"]
We can use NuGet package RouteDebugger
Install-Package RouteDebugger
Web.config: <add key="RouteDebugger:Enabled"
value="true" />
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
All requests are mapped to a specific action
Every controller should inherit Controller class
49
Action Results
Controller action response to a browser
request
Inherits from the base ActionResult class
50
Action Results (2)
51
Action Parameters
ASP.NET MVC maps the data from the HTTP
request to action parameters in few ways:
Routing engine can pass parameters to actions
http://localhost/Users/NikolayIT
Routing pattern: Users/{username}
URL query string can contains parameters
/Users/ByUsername?username=NikolayIT
HTTP post data can also contain parameters
52
Action Selectors
ActionName(string name)
AcceptVerbs
HttpPost
HttpGet
HttpDelete
HttpOptions
…
NonAction
RequireHttps
We can override:
OnActionExecuting(ActionExecutingContext)
OnActionExecuted(ActionExecutedContext)
OnResultExecuting(ResultExecutingContext)
OnResultExecuted(ResultExecutedContext)
We can applyour new attribute to a controller,
method or globally in GlobalFilters.Filters
55
Custom Action Filter (2)
public class LogAttribute : ActionFilterAttribute
{
public override void OnActionExecuting
(ActionExecutingContext filterContext) { /* */ }
[Log]
public class DepartmentController : Controller
{ // ... }
56
Razor Views
Views
HTML templates of the application
Generated
Template Data
Output
UsersController.cs
UserModel.cs
62
Razor Syntax
@ – For values (HTML encoded)
<p>
Current time is: !!!
Not HTML encoded value:
</p>
<b> , </b>
</div>
64
Razor Syntax (3)
Comments
@*
A Razor Comment
*@
//A C# comment
/* A Multi
line C# comment
*/
<p>
This is the sign that separates email names from domains: @@<br />
And this is how smart Razor is: spam_me@gmail.com
</p>
65
Razor Syntax (4)
@(…) – Explicit code expression
<p>
Current rating(0-10): / 10.0 @* 6 / 10.0 *@
Current rating(0-1): @* 0.6 *@
spam_me@Model.Rating @* spam_me@Model.Rating *@
spam_me @* spam_me6 *@
</p>
<p> </p>
66
Layout
Define a common site template
Similar to ASP.NET master pages (but better!)
Razor view engine renders content inside-out
68
Sections
You can have one or more "sections" (optional)
They are defined in the views:
72
Custom Helpers
Write extension methods for the HtmlHelper
73
Custom Helpers (2)
Another way to write helpers:
Create folder /App_Code/
Create a view in it (for example Helpers.cshtml)
Write a helper in it using @helper
JavaScript
HTML5
CSS3
jQuery
80
What Does Kendo UI Provide?
Rich UI Widgets
Templating
Validation Framework
Server wrappers
ASP.NET MVC
Java and PHP
82
Include Kendo UI in Project
Download it and unzip it (ask us for license)
83
Include Kendo UI in Project (2)
Reference Kendo UI scripts and styles
In Views\Shared\_Layout.cshtml <head>
84
Using Kendo UI
Pure HTML and JavaScript
курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн
уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET
http://schoolacademy.telerik.com
програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезания
курсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране
free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране
безплатен курс "Качествен програмен код"
безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
Free Trainings @ Telerik Academy
“C# Programming @ Telerik Academy
csharpfundamentals.telerik.com