Download - 2010 - Basta!: REST mit ASP.NET MVC
Daniel Fisher – [email protected]
MVC den REST geben
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®
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
Efficient Communication…
Agenda
• ASP.NET MVC
• REST
• REST with ASP.NET MVC
• Summary
WHAT IS 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!
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…
Nice URLs
• Vs
Process
The MVC Pattern
• The request hits the controller
• Loads the model
• Returns the view to return to the client
Controller
Model
View
Strongly Typed
<%= DisplayFor(m=>m.Title) %>
Instead of
<%# Eval("Title") %>
Concepts cont.
• No Code-Behind for ASPXs
• No Postbacks
• No Viewstate
• No controls out of Redmond!
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
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 = ""
});
Actions
public ActionResult Index()
{
return View();
}
Passing Data
public ActionResult Index()
{
ViewData["Message"] =
"Welcome to ASP.NET MVC!";
return View();
}
Passing Typed Data
public ActionResult Index()
{
var p = new Person();
return View(p);
}
Model Binder
public ActionResult Save(Person p)
{
// No need to cope with Form/QueryString
return View();
}
ASPX Views
Untyped
<%@ Page Inherits="ViewPage<dynamic>" %>
Typed
<%@ Page Inherits="ViewPage<Product>" %>
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)
WHAT IS REST?
What is REST?
• A term coined by Roy Fielding
• Style of architecture
• Resource based
• Cacheable
• NOT SOAP, NOR SESSION
• Web Standards!
What is REST
• Every Resource has a unique URI
– But it's NOT about CRUD!
• Relies on HTTP verbs
• Deliver Content as Requested
Action Attributes
• HttpPost, HttpGet, HttpDelete,
RequiresHttps
• Authorize
• ...
Action Filters
public class MyFilter
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{// Your code here…
}
}
Action Results
public class ModelResult<T>
: ActionResult
{
public override void ExecuteResult(
ControllerContext context)
{
var r = context.HttpContext.Response;
r.Write(…);
}
}
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