asp mvc s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

19
ASP MVC http://www.asp.net/mvc/t utorials/mvc-4/getting-s tarted-with-aspnet-mvc4/ intro-to-aspnet-mvc-4

Post on 19-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 2: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 2

Introduction (1) MVC (Model View Controller) uses a

common design (architectural) pattern to build web applications We could have an endless talk about

design patterns Refer to the GOF

It’s not a new concept. MVC was introduced in the 1970

Page 3: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 3

Introduction (2) The model gets requests from the

controller and sends back data The model knows things

The view gets data from the control and is responsible for rendering The view shows the things the model

knows

The model implements application logic

Page 4: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 4

Introduction (3) The controller plays the largest role

It sends requests to the model and gets back data

That data is passed on to the appropriate view

It gets and interprets requests from clients

It gets commands from the users and tells the view what to show and the model what to do

Page 5: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 5

MVC and ASP MVC 4 is the version supplied with VS

2012 MVC 5 is supplied with VS 2013

Page 6: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 6

MVC and ASP You can use Razor to build the user

interface We can also use ASP forms

It’s a very different beast than ASP.NET Web forms There is no view-state for example

MVC and Web Forms can be used in the same application

Page 7: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 7

Reasons to use MVC Easy to test Component-based Modules are very plug and play

There is no view state or server forms It’s up to you

Page 8: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 8

Reasons NOT to use MVC It’s complicated You must live in the MVC structure

Page 9: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 9

MVC The ModelFrom Wikipedia

Page 10: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 10

MVC (URLs) You are used to URLs pointing to a

physical resources An HTML document An aspx file Or something….

URLs work much differently in MVC We route names to physical URLs By default, the parts of a URL are special

If this sounds restful – it is.

Page 11: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 11

MVC Routes (1) Routes provide the mechanism to map

URLs into a corresponding call to a controller function

Roughly speaking, the format of a route is

{controller}/{action}/{id}

Page 12: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 12

MVC Routes (2)

Page 13: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 13

MVC Views Views provide the display engine in the

MVC model You create them in two ways

As traditional ASP.NET Web forms Using Razor

In a sentence, Razor is really not much more than mutated PHP

Page 14: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 14

The MVC Model (2) Model properties and method

Page 15: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 15

MVC Controllers The controller does the work It reads and writes data from the model

(via properties and methods It sends data to the view

Page 16: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 16

Create an MVC Application (1)

Page 17: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 17

Create an MVC Application (2)

Page 18: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 18

MVC Application (Structure) First, the system calls Application.Start This sets up the “routings”

Page 19: ASP MVC  s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4

Slide 19

Application startup Register routes sets up the default

controller We usually need not mess with it