What is ASP.NET MVC

A framework that maps requests onto controller classes and methods
Razor and WebForms view engines for rendering HTML templates
extensible, customizable and designed with testing in mind

System.Web
System.Web.Routing
 System.Web.Mvc

 ASP.NET MVC 1.0 - 2009 Mar 13 
 ASP.NET MVC 2.0 - 2010 Mar 10 
 ASP.NET MVC 3.0 - 2011 Jan 13
 ASP.NET MVC 4.0 - 2012 Aug 15

MVC is open source


 Microsoft Public License (MS-PL)

Pull requests accepted

MVC code is available for browsing/debugging/modification/redistribution

At http://aspnet.codeplex.com/

WHY MVC?

instead of web forms
Lack of control over rendered markup
many server controls generate ugly HTML
client side CSS and JavaScript sometimes cumbersome
Lack of modularity 
pages contain input processing and rendering logic
pages sometimes even contain data access logic
Lack of control over runtime environment dependencies
applications are difficult to test without live web server
Lack of control over incoming URLs

Model View Controller 

MVC steers you in the "correct" direction.
It makes it hard to put code in the "wrong" place 

Controller handles the incoming request

Controller "returns" a View (View Engine generated)

Controller likely uses a Model when returning the view

Model usually is the "data context" for the View

MVP vs MVC

MVC Components

Controller is the brains
  accesses model
  chooses response or view
  same as WebForms (but more explicit)
  no data access layer calls in pages
  only contains rendering logic
  passed model/data from controller

  receives requests, processes all input


Model is business data

View emits response


WEB API


This is just a different type Controller that is optimized for exchanging data as opposed to Views

Inherits from ApiController

Creates a new Route in the Route Table
(usually /api/controllerName)


Web API cont..

Controller methods should return data not Views

Controller following standard REST conventions


Support default content negotiation. Respects accept header with valid content-type responses 

Advanced asp.net


Model Binding

Filters

Areas

Bundles

Nested Layouts

Partial Views

model binding

Model is passed to the View from the Controller

Your Model is typically a strongly typed C# Object

In the Controller: return View(InstanceOfModel);

In the View: @model ModelType

<h2> @Model.Property </h2>

The Model object is bound to the view and accessible to the Razor Template Engine.

model binding cont..

Can create "ViewModels". 

These are Model objects that are specifically tied to the View.

Rather than returning your Entity directly you return a flattened object that is designed for that specific view. 

bundles

New to ASP.NET MVC4

Gives you a way to group like files into a single "bundle" for use on your page.

Automatically minifies or uses minified version when debug=false in web.config (or EnableOptimizations is true)

Can specify a CDN path for release and local copy of debug

Can use wildcards.

Action Filters


Additional way to manage access to a controller method.

Often used to require login, require ssl, etc..

Place the attribute above the Action or the whole controller

eg: [Authorize]

Can create your own custom filters eg: [RequireMyCustomCookie]

areas

Gives you a way to break up complicated sites in to smaller functional "areas"

Subfolder structure to hold Controllers, Views, Registration etc.. for the Area.

Call AreaRegistration.RegisterAllAreas() to "bootstrap" the areas into the global handler

Routes handled by the area itself.

Right Click | Add New Area | Scaffolding adds stub code
Made with Slides.com