32
Daniel Fisher [email protected] MVC den REST geben

2010 - Basta!: REST mit ASP.NET MVC

Embed Size (px)

Citation preview

Page 1: 2010 - Basta!: REST mit ASP.NET MVC

Daniel Fisher – [email protected]

MVC den REST geben

Page 2: 2010 - Basta!: REST mit ASP.NET MVC

devcoach.com

• Leistungen– Beratung und Projektbegleitung

– Software-Entwicklung

– Training

• Technologien– ASP.NET Web Forms & MVC

– WCF & WF

– Silverlight & MonoTouch

• Kunden– Versicherung, Finanzindustrie, Mittelstand, Handel,

Kommunikation, Softwarehersteller u.v.a.

• Bundesamt für Sicherheit in der Informationstechnologie,

• Microsoft

• Dresdner Bank

• Und Sie?

Project Experience

Technology Know-how

devcoach®

Page 3: 2010 - Basta!: REST mit ASP.NET MVC

Daniel Fisher

• devcoach.com– Mit-Gründer und Geschäftsführer

• Justcommunity.de– Mit-Gründer und Vorstand

• nrwconf.de– Mit-Gründer und Organisator

• netug-niederrhein.de– Mit-Gründer und Leiter

• microsoft.com– Business Technology Platform Advisor

– Community Leader & Insider (CLIP)

– Certified Professional Developer

• lennybacon.com– Blog

• twitter.com– @lennybacon

Page 4: 2010 - Basta!: REST mit ASP.NET MVC

Efficient Communication…

Page 5: 2010 - Basta!: REST mit ASP.NET MVC

Agenda

• ASP.NET MVC

• REST

• REST with ASP.NET MVC

• Summary

Page 6: 2010 - Basta!: REST mit ASP.NET MVC

WHAT IS ASP.NET MVC

Page 7: 2010 - Basta!: REST mit ASP.NET MVC
Page 8: 2010 - Basta!: REST mit ASP.NET MVC

Büttenrede

Der ViewState is ne falsche Sache - Mir

wolle was ganz and'res mache!

Code-Behind mit 11 tausend Lines of

Code - Das ist doch der Maintenance-

Tod!

Post-Back und Controls im Web, dass

kannst'e doch nich' testen, Depp!

Page 9: 2010 - Basta!: REST mit ASP.NET MVC

Concepts

• Nice URLs & Routing

• Process over „just add a Form“

• Loads of naming conventions

• Code, Code, Code

• As strongly typed as possible!

• Once build, forever used…

Page 10: 2010 - Basta!: REST mit ASP.NET MVC

Nice URLs

• Vs

Page 11: 2010 - Basta!: REST mit ASP.NET MVC

Process

Page 12: 2010 - Basta!: REST mit ASP.NET MVC

The MVC Pattern

• The request hits the controller

• Loads the model

• Returns the view to return to the client

Controller

Model

View

Page 13: 2010 - Basta!: REST mit ASP.NET MVC

Strongly Typed

<%= DisplayFor(m=>m.Title) %>

Instead of

<%# Eval("Title") %>

Page 14: 2010 - Basta!: REST mit ASP.NET MVC

Concepts cont.

• No Code-Behind for ASPXs

• No Postbacks

• No Viewstate

• No controls out of Redmond!

Page 15: 2010 - Basta!: REST mit ASP.NET MVC
Page 16: 2010 - Basta!: REST mit ASP.NET MVC

IIS sends Response

ViewResult.Render

ViewResult locates

corresponding View

ActionResult.

ExecuteResult

ActionResult builds

ViewData / Model

Controller.{Action}Controller.ExecuteIControllerFactory

IHttpHandler.

ProcessRequest

MvcRouteHandler

(IHttpHandler)

IRouteHandler.

GetHttpHandler

URLRoutingModule

IIS takes Request

Page 17: 2010 - Basta!: REST mit ASP.NET MVC

Defining Routes

routes.Ignore("*.svc");

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new {

controller = "Home", action = "Index", id = ""

});

Page 18: 2010 - Basta!: REST mit ASP.NET MVC

Actions

public ActionResult Index()

{

return View();

}

Page 19: 2010 - Basta!: REST mit ASP.NET MVC

Passing Data

public ActionResult Index()

{

ViewData["Message"] =

"Welcome to ASP.NET MVC!";

return View();

}

Page 20: 2010 - Basta!: REST mit ASP.NET MVC

Passing Typed Data

public ActionResult Index()

{

var p = new Person();

return View(p);

}

Page 21: 2010 - Basta!: REST mit ASP.NET MVC

Model Binder

public ActionResult Save(Person p)

{

// No need to cope with Form/QueryString

return View();

}

Page 22: 2010 - Basta!: REST mit ASP.NET MVC

ASPX Views

Untyped

<%@ Page Inherits="ViewPage<dynamic>" %>

Typed

<%@ Page Inherits="ViewPage<Product>" %>

Page 23: 2010 - Basta!: REST mit ASP.NET MVC

Event-driven RESTful

ASP.NET RoR, MonoRail, ASP.NET

MVC

Stateful Stateless

Heavyweight Lightweight

Overcomes HTTP Embraces HTTP

Widgets HTML

3rd-party controls DIY

Design view Code view

GUI Web page

Postbacks POSTs

Web 1.0 (uncool) Web 2.0 (cool)

Page 24: 2010 - Basta!: REST mit ASP.NET MVC

WHAT IS REST?

Page 25: 2010 - Basta!: REST mit ASP.NET MVC
Page 26: 2010 - Basta!: REST mit ASP.NET MVC

What is REST?

• A term coined by Roy Fielding

• Style of architecture

• Resource based

• Cacheable

• NOT SOAP, NOR SESSION

• Web Standards!

Page 27: 2010 - Basta!: REST mit ASP.NET MVC

What is REST

• Every Resource has a unique URI

– But it's NOT about CRUD!

• Relies on HTTP verbs

• Deliver Content as Requested

Page 28: 2010 - Basta!: REST mit ASP.NET MVC
Page 29: 2010 - Basta!: REST mit ASP.NET MVC

Action Attributes

• HttpPost, HttpGet, HttpDelete,

RequiresHttps

• Authorize

• ...

Page 30: 2010 - Basta!: REST mit ASP.NET MVC

Action Filters

public class MyFilter

: ActionFilterAttribute

{

public override void OnActionExecuting(ActionExecutingContext filterContext)

{// Your code here…

}

}

Page 31: 2010 - Basta!: REST mit ASP.NET MVC

Action Results

public class ModelResult<T>

: ActionResult

{

public override void ExecuteResult(

ControllerContext context)

{

var r = context.HttpContext.Response;

r.Write(…);

}

}

Page 32: 2010 - Basta!: REST mit ASP.NET MVC

Summary

• ASP.NET MVC offers an alternative to

WCF WebHTTP features

– It's more Web

• Fully integrated into a web application

– It's less Services

• No other protocols

• No WSDL

• No Proxies