Getting Started with MVC 5 and Visual Studio 2013

Slides:



Advertisements
Similar presentations
Introduction to ASP.NET MVC
Advertisements

INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
{ Model View Controller ASP.NET By Scott Crooks & Maggie Wettergreen.
Web Applications Development Using Coldbox Platform Eddie Johnston.
ASP.NET MVC Best Practices Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009.
ASP MVC s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4.
Web Development Using ASP.NET CA – 240 Kashif Jalal Welcome to week – 1.1 of…
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
UNIT-V The MVC architecture and Struts Framework.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Windows.Net Programming Series Preview. Course Schedule CourseDate Microsoft.Net Fundamentals 01/13/2014 Microsoft Windows/Web Fundamentals 01/20/2014.
Ori Calvo, 2010 “If people want to have maximum reach across *all* devices then HTML will provide the broadest reach” Scott Guthrie,
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
JavaScript & jQuery the missing manual Chapter 11
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
ASP.NET and Model View Control Jesper Tørresø ITNET2 F08.
ASP.NET Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
Introducing ASP.NET 2.0. Internet Technologies WWW Architecture Web Server Client Server Request Response Network HTTP TCP/IP PC/Mac/Unix + Browser (IE,
Module 4: Creating a Microsoft ASP.NET Web Form. Overview Creating Web Forms Using Server Controls.
Getting Started with ASP.NET MVC BRIJ BHUSHAN MISHRA.
Model View Controller MVC Web Software Architecture.
Getting started with ASP.NET MVC Dhananjay
ASP.NET (Active Server Page) SNU OOPSLA Lab. October 2005.
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Virtual techdays INDIA │ 9-11 February 2011 SESSION TITLE Kamala Rajan S │ Technical Manager, Marlabs.
Navigation Framework using CF Architecture for a Client-Server Application using the open standards of the Web presented by Kedar Desai Differential Technologies,
Introduction to ASP.NET development. Background ASP released in 1996 ASP supported for a minimum 10 years from Windows 8 release ASP.Net 1.0 released.
The New Face of ASP.NET ASP.NET MVC, Razor, and jQuery Ido Flatow | Senior Architect | Sela | This session is.
INTRODUCTION TO MVC BY SUHA MNEIMNEH. WHAT’S THE AGENDA What is MVC? MVC components MVC vs web forms vs ASP.NET vocabulary When to create MVC application.
Introduction to MVC Slavomír Moroz. Revision from Previous Lesson o ASP.NET WebForms applications Abstract away HTTP (similar to desktop app development)
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Jim Fawcett CSE686 – Internet Programming Spring 2014
ASP.NET Forms.
Introduction to .NET Florin Olariu
An introduction to ASP.Net with MVC Nischal S
Jim Fawcett CSE686 – Internet Programming Spring 2012
Social Media And Global Computing Introduction to The MVC Pattern
MVC Architecture, Symfony Framework for PHP Web Apps
ASP MVP Web applications and Razor
ASP.NET MVC Introduction
Play Framework: Introduction
MVC and other n-tier Architectures
Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek
Haritha Dasari Josue Balandrano Coronel -
PHP Training at GoLogica in Bangalore
PHP / MySQL Introduction
Understand Windows Forms Applications and Console-based Applications
SharePoint Cloud hosted Apps
ASP.NET Web Forms Vs. ASP.NET MVC ASP.NET is Microsoft’s big leap after Active Server Pages (ASP), ASP.NET provides flexible and unified web development.
Chapter 23 – ASP.NET Outline 23.1 Introduction NET Overview
Model-View-Controller Patterns and Frameworks
Controllers.
Lecture 1: Multi-tier Architecture Overview
ASP.NET
ASP.NET Module Subtitle.
Web Development Using ASP .NET
An Introduction to JavaScript
Model, View, Controller design pattern
ASP.NET and Model View Control
Web Client Side Technologies Raneem Qaddoura
Web Application Development Using PHP
Presentation transcript:

Getting Started with MVC 5 and Visual Studio 2013 Thomas Robbins, Kentico CMS (thomasr@Kentico.com) @trobbins Slides will be available at: http://bit.ly/thomrobbins

What we will talk about This is an introductory session on MVC 5 and VS 2013 is designed to get you going. We’ll look at the Why of MVC vs Web Forms  We’ll also look at some best practices and things to think about Lots of other great sessions and information available.

The Strength of ASP.NET Web Forms A history lesson.. The Strength of ASP.NET Web Forms Making web development feel the same as Windows Form development No need to work with individual HTTP requests and easier to think in terms of a stateful UI ASP.NET Web Form A set of UI components (pages, buttons etc.) plus a stateful object oriented GUI programming model ASP.NET A way to host .NET application in IIS that let’s you interact with HTTP requests and responses .NET A multi-language managed code platform

Some problems with ASP.NET Web Forms View state weight – Mechanism for maintaining state (view state) results in large blocks of data between client and server Page life cycle – Connecting client side events with server side event handler code is complicated and delicate False sense of separation – ASP.NET web Forms code behind model provides a means to take application code out of HTML markup. Unfortunately, this allows for mix presentation models (manipulating a server side tree) Limited control over HTML – Server side controls render themselves as HTML but not always the HTML you want Low testability – Nobody could have anticipated that automated testing would become essential Not all bad – ASP.NET Web Forms provide a quick results and allows reasonably complex web applications to be built quickly!

What matters… Code reusability Separation of concerns Shortens development Code libraries Design patterns Frameworks Improves code clarity and organization Helps troubleshoot by isolating issues Allows for multiple teams to develop simultaneously Application A Application B

Say hello to MVC!

It’s a pattern Model: Handles data and business logic View: Presents data to the user using any supported form and layout Controller: receives user requests and calls appropriate resources to carry them out

What is MVC? Model represents the data model “Manages behavior and data of the application domain” View represents the screen shown to the user “Manages the graphical and/or textual output to the portion of the bitmapped display that is allocated to the application” Controller represents interaction from the user that changes the data and the view “Interprets the mouse and keyboard inputs from the user, commanding the model and/or the view to changes as appropriate”

MVC isn’t new! Presented by Trygve Reenskaug in 1979 First used in the Smalltalk-80 framework Used in making Apple interfaces (Lisa and Macintosh)

Step by Step Model 2 1 3 Browser Controller 4 View 5 The Controller asks the Model for data The request hits the controller Model 2 1 3 Browser Controller The Model gives the data back to the Controller The controller formats the data and passes them to the View 4 View 5 The view renders the HTML that needs to be sent to the client

Example control flow in MVC User interacts with the View UI Controller handles the user input (often a callback function attached to a UI element) Controller updates the Model View uses the model to generate new UI UI waits for user interaction

What’s the point? Provides a logical structure for heavily interactive system Adheres to good engineering design principles and practices Information hiding, less coupling, simplicity, etc. Delegated control style

Getting started with MVC 5

The project structure App_Data is the physical store for data. This folder has the same role as it does in ASP.NET web sites that use Web Form pages Content is the recommended location to add static content files like CSS and images Controllers is the recommended location for controllers. All controllers must end with “Controller” Models is provided for classes that represent the application model. This folder usually contains code that defines objects and logic for application with the data store Scripts is the recommended location for script files that support the application. By default this folder contains ASP.NET Ajax Foundation files and Jquery Views is the recommended location for views. These are ViewPage (.aspx), ViewUserControl (.ascx) and ViewMaster (.master) in additional to any other files needed for renderings. The view folder also contains a folder for each controller.

Everything has it’s advantages MVC WebForms Easier to Manage Complexity Does not use view state or server based forms Rich Routing Structure Support for Test-Driven Development Supports Large Teams Well Preservers State over HTTP Page Controller Pattern View state or server based forms Works well for small teams Development is less complex

The beauty of MVC It’s Restful!

MVC Routes A route is an object that parses a requested URL and it determines the controller and action to which the request is forwarded Routing operates on the directories and the file name of tin the relative URL Uses the format /[Controller]/[ActionName]/[Parameters]

What’s the route Matching a URL request to a route depends on all of the following conditions: The route patterns that you have defined or the default route patterns, if any, that are included in your project type. The order in which you added them to the Routes collection. Any default values that you have provided for a route. Any constraints that you have provided for a route. Whether you have defined routing to handle requests that match a physical file.

Models

What is a model The model should contain all of the application business logic, validation logic, and database access logic. Supports a code first model using the Entity Framework (EF) All .edmx files, .dbml files etc. should be located in the Models folder.

Custom view model When you combine properties to display on a View Similar problem with ASP.NET Webforms…

View

What is a View Most of the Controller Actions return views The path to the view is inferred from the name of the controller and the name of the controller action. \Views\ControllerName\ControllerAction.aspx A view is a standard (X)HTML document that can contain scripts. script delimiters <% and %> in the views

Passing data to a view With ViewData: ViewData["message"] = "Hello World!"; Strongly typed ViewData: ViewData.Model = OurModel; With ViewBag: ViewBag.Message = "Hello World!";

Post data to a controller Verb Attributes The action method in the controller accepts the values posted from the view. The view form fields must match the same names in the controller. [HttpPost] public ActionResult Edit(Meeting meeting) { if (ModelState.IsValid) db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(meeting);

Controller

What is a controller It’s a class derived from System.Web.MVC.Controller class Generate the response to the browser request public class HomeController : Controller { public ActionResult Index() ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About()

Controller actions Public method of the Controller class Cannot be overloaded Cannot be a static method Returns action result public class HomeController : Controller { public ActionResult Index() ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About()

Other Features Scaffolding Test Driven Development Internationalization Many More

MVC Best Practices

Delete AccountController.cs MVC best practice #1 Delete AccountController.cs Why You will probably never use these account management pages Keeping demo code in production application is not a good practice

Isolate controller from the external world MVC best practice #2 Isolate controller from the external world HTTPContext Data access classes Configuration management Logging Etc.. Why Increases testability of your application Increases flexibility of your application

Avoid “magic strings” View best practice #3 A magic string is an input that a programmer believes will never come externally and which activates hidden functionality. A user would likely provide input that gives unexpected responses in most situations. However, if the user innocently provides a pre-defined input, invoking external functionality, the program response is unexpected (“it’s magic”) What should you do Avoid using ViewData[“key”] Always create a ViewModel for each View and inherit from “System.Web.MVC.ViewPage<ListViewModel>

Get creative and personalize your experience MVC best practice #4 Get creative and personalize your experience What should you do ASP.NET MVC is the base on which to build your own reference architecture Controllers and views inherit from your own base class Always create a ViewModel for each View and inherit from “System.Web.MVC.ViewPage<ListViewModel>

Choose your view engine carefully MVC best practice #5 Choose your view engine carefully What’s a view engine? A view engine is responsible for rendering HTML from your views to the browser. Why? Default is WebFormViewEngine and may not be the best Choose the one that makes the most sense for you View Engine Description Razor The Razor view engine is an advanced view engine from Microsoft. Razor using an @ character instead of aspx's <% %> and Razor does not require you to explicitly close the code-block, this view engine is parsed intelligently by the run-time to determine what is a presentation element and what is a code element. ASPX The syntax for writing views with this engine is the same syntax that the ASP.NET Web Forms uses and the file extensions are also taken from ASP.NET Web Form (.aspx, .ascx, .master) . The coding will give us the memory of legacy ASP style. And many more….

Avoid logic in your views MVC best practice #6 Avoid logic in your views What can you do While it is allowed to have “if” and “For Each” when possible hide them in an HTMLHelper Represents support for rendering HTML controls in a view Just a string that returns a string that can represent any type of content that you want MVC Framework includes standard HTML helpers HTML.ActionLink() HTML.BeginForm() HTML.Checkbox() HTML.Dropdown() HTML.EndForm HTML.Hidden HTML.ListBox HTML.Password() HTML.RadioButton HTML.TextArea HTML.TextBox

MVC Best Practice #7 When an ASP.NET MVC Web application runs in IIS 7.0, no file name extension is required for MVC projects. However, in IIS 6.0, the handler requires that you map the .mvc file name extension to the ASP.NET ISAPI DLL.

Pay attention to verbs MVC best practice #8 What happens when you refresh or submit a form? Leverage the Post/Redirect/Get (PRG) Patter View sends data in POST Modify data in POST Controller validates Renders the View with errors (POST) Redirect in GET View renders the results in GET Show data in GET

Wrap up… MVC is not the only solution but becoming increasingly the answer VS 2013 has MVC 5 ready to go

Who are we? Kentico CMS is one of the most powerful Customer Experience Management Systems and Content Management Systems on the planet. With over 16,000 web sites in 90 countries it is used for everything from simple web sites to complex applications. Kentico CMS is easy to install, simple to manage and reliable.