As I mentioned in a previous post, I have recently started working on iPhone Application development. One of the many ways an application can interact with data is via web services. As a (primarily) .NET developer, I have been looking for a way to create a lightweight RESTful API that will return JSON. One thing is for sure, things like ASMX which use SOAP will not do. So what is a developer to do?
Enter Martin
Martin is a web framework that allows for building REST services in ASP.NET. For a little history, Martin is based off Sinatra, a Ruby framework that does essentially the same thing; saying every method defines a route. A route (URI + Verb) invokes a single method. Martin is essentially built the same way by relying on System.Web.Routing from .NET 3.5 Service Pack 1. In short you can build your own DSL (domain-specific language) and easily implement a version of it into your currently existing ASP.NET or ASP.NET MVC web site.
Implementing Martin
While, unfortunately, there is no actual documentation, the code mostly explains itself as long as you understand the basics of HTTP Verbs and URL Routing. The implementation process of the Martin framework is extremely simple, and takes only a few steps.
Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
Config.AddAction<DateAction>("Show/CurrentDate");
Config.AddAction<PostDateAction>("Show/CurrentDateWithPost");
}
The URI “http://example.com/Show/CurrentDate” points to the class DateAction. This class has a single method that reflects one of four HTTP Verbs (Get, Post, Put, Delete), and will either return pure text, XML, or JSON. As a bonus, you can return custom objects in XML and JSON format, no manual writing involved! For example DateAction.cs would look like…
public class DateAction : ActionBase
{
public override IResponse Get()
{
return Text(DateTime.Now.ToString()));
}
}
So in short “http://example.com/Show/CurrentDate” is an HTTP Get that will return just the text of the current date and time. That said, there will be times you want to do either an HTTP POST and/or pass variable into your web service. For this I have created the example PostDateAction:
public class PostDateAction : ActionBase
{
public override IResponse Post()
{
if (!String.IsNullOrEmpty(ValueOf<String>("name")))
{
return Text(string.Format("The current date is {0}, {1}.", DateTime.Now, ValueOf<String>("name")));
}
return Text(string.Format("The current date is {0}.", DateTime.Now));
}
}
In this example, we can optionally pass in the variable “name.” In order to get the value of that parameter, we use the ValueOf(String key) function to retrieve the passed in name, and do something with it. Also, the ActionBase class also allows you to access RouteData or Request items that might be part of the request.
Unfortunately, there is not much documentation on this framework; with that said, it is written very well and can easily be interpreted and implemented. I was able to get a working demonstration of various HTTP Verbs and return types in a matter of 5-10 minutes.
The Martin framework can be downloaded from github, as it was recently moved there from Google Code. Let both the author and me know what you think.
In the mean time, happy coding!
Source: http://github.com/thegrubbsian/Martin