ASP.NET MVC Fundamental

Post on 15-Apr-2017

5.634 views 0 download

Transcript of ASP.NET MVC Fundamental

ASP.NET MVC 3 Training for Heroes WELCOME TO MVC SHINE DAY

Lê Dương Công Phúc

SESSION 1: ASP.NET MVC FUNDAMENTAL

Who am I?

• Lê Dương Công Phúc• Technical evangelism• Software engineer at Vina Digital Co,. Ltd.

Active Server Pages• Visual Studio 98:

– ASP, VBScript, ADO– Request, Response object

Evolution of ASP.net #2: ASP 1.0, 1.1, 2.0

• ASP.net 1.0: January 16th 2002• ASP.net 1.1: April 24th 2003• ASP.net 2.0: Nov 7th 2005, Visual Studio 2005

– New data controls (GridView, FormView, DetailsView)– Declarative data access (SqlDataSource, ObjectDataSource, XmlDataSource controls)– Navigation controls– Master pages– Login controls– Themes– Skins– Web parts– Personalization services– Full pre-compilation– New localization technique– Support for 64-bit processors– Provider class model

Code Behind,

Web Form,

ViewState

ASP.net 3.0, 3.5, 3.5 SP 1

• ASP.net 3.0: Nov 21 2006 : WCF, CardSpace• ASP.net 3.5: Nov 19 2007, released with VS2008:

– LINQ Data Source– New data controls (ListView, DataPager)– ASP.NET AJAX included as part of the framework– Support for HTTP pipelining and syndication feeds.– WCF Support for RSS, JSON, POX and Partial Trust

• ASP.net 3.5 sp1: Aug 11 2008:– Dynamic Data– Browser history in an ASP.NET AJAX– Combine multiple Javascript files into a single file– New namespaces System.Web.Abstraction and System.Web.Routing

ASP.NET 4.0• Easier to get started, develop and deploy• Routing and Search Engine Optimization• Productivity & Extensibility

The Microsoft Web Platform

Web

Pla

tform

Inst

alle

rAjax Control Toolkit & jQuery

ASP.NET

ADO.NET Entity Framework

SQL Server

IIS

The Microsoft Web Platform combines a rich and powerful web application framework with a supporting cast of tools, servers, technologies and applications for creating, designing, developing and delivering web solutions.

Web Platform Installer

• Makes it easy to install the Microsoft Web Platform from one place– Framework, Web Server, Database and Tools

• Cost = free; Size < 2MB• Always has latest version of the platform available• Available in 9 languages• Web App Gallery– Umbraco, DotNetNuke, Drupal, WordPress and many

more– Submit your own apps, get distribution

Today’s Web Developers

I <3 Web Apps. I just need a tool that makes them easier to

configure, customize and publish them

I want to build web sites myself with an

easy to learn tool and framework

I’m a professional software developer and

I build complex, large scale web sites with a

team of developers

Goals of ASP.NET MVC

• Clean URL schemes• Support existing ASP.NET runtime features• Build on top of System.Web, doesn’t replace it• Enable clean separation of concerns• Testable by default (built with TDD in mind)• Support third-party view engines• Auto-mapping of form variables to object properties• Provides complete control over your HTML markup• Enables rich JavaScript, AJAX integration

Key Concepts

MvcHandler Controller

ControllerFactory

Action

Model

ViewResult

ActionResult

View

ViewEngine

forwards creates

invokes

uses

produces

defines

renders

produces

MVC Pattern

Controller

Model

View

Request

Response

Data Tr

ansfer

Data

Tra

nsfe

r

Business InteractionLayer

User InteractionLayer

Binds the model and view together and selects which view

to display next

Decouples the backend business

logic from the front end

Visualizes the application data supplied by the

model

Even

t Noti

ficati

on

How MVC Works

• What does MVC look like?

Request

View

Controller

Response

ControllerRetrieves Model“Does Stuff”

ViewVisually representsthe model

Introducing “MVC Shine” application

• Features– View detail about asp.net mvc activities of heroes– Rate an activity

• What you are doing to see today– Build the first ASP.NET MVC application– ASPX view engine– Learning about ASP.NET MVC 3 fundamental– Razor view engine– Implementing simple functionalities (CRUD, master-

detail)

Controllers

Controllers handle requests from the userControllers create the model for the viewsApplication logic is placed in the controllers

Request ViewController

Models

The models hold the application dataModels are the mean of communication between the controllers and the viewsModels hold no application logic

ViewController

Views

• Views are visual representations of a model• There can be many views for the same model• Views contain no application logic

View

URL routing

• Web form: without query string, an URL maps to one aspx page

• MVC: URL maps to Controller/Action/RoutesValues– /Company/Details/3

• Controller = CompanyController• Action = Details• Route Value = {ID = 3}

– /News• Controller = NewsController• Action = Index• Route Value = null

Action Result

• The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing.

• The most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.

Action Result Examples

• Return JSON[AcceptVerbs(HttpVerbs.Get)]public ActionResult GetSubSkill(int id){ return Json(db.GetSkills(id)); //Return data in JSON format}$.getJSON("/Skill/GetSubSkill/" + $(this).attr("id"), function(data) {

• Returns JavaScript[AcceptVerbs(HttpVerbs.Get)]public JavaScriptResult JAction(){ return JavaScript("alert('HelloWorld')");}…<script type="text/javascript" src="/Home/JAction"></script>

MVC UI Helper Controls

• The HtmlHelper class renders Html fragments of views.

• Basic MVC UI Helper ControlsHtml.ActionLink(), Html.BeginForm(), Html.CheckBox(), Html.DropDownList(),

Html.EndForm(), Html.Hidden(), Html.ListBox(), Html.Password(), Html.RadioButton(), Html.TextArea(), Html.TextBox()

• MVC UI Helper Controls can be extended using extension method.http://www.asp.net/Learn/mvc/tutorial-09-cs.aspx

Framework Goals

• Frictionless Testability• Tight control over <markup>• Leverage the benefits of ASP.NET• Conventions and Guidance

MVC vs Web Form

Web Form MVC

Mature technologyRich toolset & controlsEvent driven modelEasy state managementFeels like Windows developmentGood for statefull Line of Business web applications

It’s testable, do unit test easilyClear separation of concernsControl your output exactlyMap URLs logically or dynamicallySupports many web forms features (auth, caching, etc…)MVC UI Helper ControlGood for Web 2.0 web applications

MVC is just another choice of technologies

for web development using ASP.NET.

Web forms is still well developed and

supported