Lyubomir Lyubenov

.Net 4.0 RESTful Web Service – .NET and LAMP people working together

Up2 people are good professionals with deep knowledge on software practices and processes. But every one of them is more or less better in his own technology (ASP.Net, Silverlight, WordPress, Drupal, Joomla). In one of our last project we used the strengths of .Net and  LAMP people together. We used .Net 4.0 RESTful Service as business logic layer in our application and WordPress content management system as front end development. We can say that the result of combination of strength of .Net object oriented programming and deep knowledge of HTML and CSS on side of  LAMP people were successful for us.

How we implement our RESTful web service?

A RESTful web service, also called a RESTful web API, is a web service implemented using HTTP and the principles of REST.

For example I show you, how we can create a new user:

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "CreateUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

public string CreateUser(string email, string firstName, string lastName)

{

// create and validate newUser

// can write some business logic

return JsonConvert.SerializeObject(newUser);

}

Here the OperationContract attribute indicates that the method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.

With WebInvoke attribute we can say that a service operation is logically an invoke operation and that it can be called by the WCF REST programming model. In his properties we can set that the Method will be POST, because we want to create new entity in our database. The URITemplate is our Uniform Resource Identifier template for the service operation. We choose to use Json data as request and response format. The other option is XML data. The reason we use JSON is that XML is more complex than necessary for Web Service. By default, XML requires you to use complex features that many Web Service do not need to be successful. Json not only fits in well with the Web’s JavaScript programming model, but it provides a simpler, easier to understand data model for Web programmers. Ultimately the both JSON and XML can be parsed.

In the CreateUser method we can create and validate a new user, add some more business logic if it is necessary and at the end the result values will be the serialize JSON object.

Example request and result for our RESTful web service can be:

POST https://example.com/Service1/CreateUser

Post data: {“email”:”testEmail@up2.nl”, “firstName” : “Ivan”, “lastName” : “Ivanov”}

Json Result: CreateUserResult = {“password”: “804074”, “uid” : “357”}

Now our front end developers can parsed the Json result data and build our application with nice and friendly design.

Leave a comment




captcha

Please enter the CAPTCHA text