710 likes | 1.49k Views
ASP MVC. http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4. 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
E N D
ASP MVC http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
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
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
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 know
MVC and ASP • MVC 4 is the version supplied with VS 2012 • MVC 5 is supplied with VS 2013
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
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
Reasons NOT to use MVC • It’s complicated • You must live in the MVC structure
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.
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}
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
Razor Syntax (1) • Code is added using the @ character and enclosed in braces • Statements end with a semicolon • Code is case sensitive
Razor Syntax (2) • There are many redefined objects Request, Response… just as in asp.net
Razor Syntax (3) • Decision making and loops are supported
The MVC Model (1) • The model contains all of the business logic for the application • It’s just a class that contains properties and methods • These methods and properties, in turn, are referenced by the controller
The MVC Model (2) • Model properties and method
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
MVC Application (Structure) • First, the system calls Application.Start • This sets up the “routings”
Application startup • Register routes sets up the default controller • We usually need not mess with it