MVC Introduction
MVC Introduction
MVC Introduction
RAZOR ENGINE
Razor Engine
• A new view-engine
• Optimized around HTML generation
• Code-focused templating approach
Razor Engine – Design goals
• Compact, Expressive and Fluid
• Easy to learn
• It is not a new language
• Works with any text editor
• Great Intellisense
• Unit-testable
– Testing views without server, controllers…
Razor – HelloWorld
• Uses @ for razor file
Razor blocks
.aspx file
Loops and Nested HTML
Razor syntax .aspx syntax
If Blocks and Multi-line Statements
Multi-
IF statement
Token
statement
Identifying nested
content with HTML
block tag
Layout/Master page
SiteLayout.cshtml
@RenderBody()
Including specific body content.
Content page
Named section.
Named section.
Master page – result html
Re-usable “HTML Helpers”
• Methods that can be invoked
within code-blocks
• Encapsulate generating HTML
• Implemented using pure code
• Work with Razor engine
31
32 of 114
Select the project template
36
ASP .NET MVC App Home page
37
Run the application…
38
Expand the default App menu
39
ADDING CONTROLLER
37 of 114
Adding controller
41
Adding controller (cont.)
42
Adding a controller (cont.)
40
Testing the controller
41
Mapping controller
• Controller selection based on URL
• Default URL routing logic:
/[Controller]/[ActionName]/[Parameters]
• Format for routing in
App_Start/RouteConfig.cs
45 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()
46 of 114
Parameters
• /HelloWorld/Welcome?name=Scott&numtimes=4
• Introducing 2 parameters to Welcome method
• Parameters passed as query strings!
47 of 114
URL Parameters
• http://webapp/HelloWorld/Welcome/3?name=Rick
48 of 114
References
The good source for learning
http://www.asp.net/mvc/overview/getting-started/int
http://www.dotnetodyssey.com/asp-net-mvc-5-free-course/your-first-asp-net-mv
c-application/