2012-08-30
This presentation was created with reveal.js. You'll need a browser with support for CSS 3D transforms to see it in its full glory.
$.get("user/4711", function(userData){
alert("User: " + userData);
});
That’s not really RESTful, it’s just kinda RESTful!
Still not the norm
I guess we'll just have to embrace the chaos and keep building snowflakes.
ASP.NET shipped with .NET in 2002.
Now known as ASP.NET Web Forms.
Focused on websites. Windows developers felt at home with the event-driven model.
Not good for creating a Web API. In that regard, nothing much changed between version 1.0, 1.1, and 2.0.
Supported SOAP and the [WebMethod]
attribute was a simple way to turn a method into a XML Web service. Declared legacy technology by Microsoft.
Windows Communication Foundation
could use ASP.NET to host services.
Almost nobody understood how.
ASP.NET AJAX - an attempt to modernize ASP.NET.
Had JSON serialization and some even succeeded to use jQuery against it.
WCF still cumbersome to use.
Not even headbeards liked WCF
PS. WCF in .NET Framework 4 is easier to use but it's still a big framework. DS.
In 2004 Ruby on Rails was released and made it big. It was followed by several other frameworks (Django, FubuMVC, OpenRasta, Castle MonoRail) based on the MVC pattern.
An alternative to ASP.NET Web Forms.
ASP.NET MVC is a better fit for HTTP services than ASP.NET Web Forms.
Do we really need MVC for HTTP services?
Could we do with something even simpler?
Sinatra, a lightweight Ruby framework, is often used to create RESTful APIs. It's a DSL for simple web applications.
require 'sinatra'
get '/hello/:name' do |name|
"Hello #{name}!"
end
Nancy was inspired by Sinatra.
public class Module : NancyModule
{
public Module()
{
Get["/hello/{name}"] = x => {
return string.Concat("Hello ", x.name);
};
}
}
There's also Simple.Web, Manos, and more.
Sometimes these frameworks are a little bit too simple.
Service Stack has similar properties.
There's Enterprise Web Services that use SOAP and WS-*.* and they are great for many transactional or complex scenarios. Then there are lighter weight RESTful web services or "Web APIs" that use JSON, XML and respect all of the goodness and stability that is the HTTP specification.
File -> New -> Project
Web API
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post(string value)
{
}
// PUT api/values/5
public void Put(int id, string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The default start page says nothing about your application, but that's easy to change.
Open the nuget console and run:
PM> Install-Package Microsoft.AspNet.WebApi.HelpPage -Pre
ASP.NET Web API is bundled with ASP.NET MVC 4.
For Visual Studio 2010 you can download it at www.asp.net/mvc/mvc4.
It's included in Visual Studio 2012.