65.9K
CodeProject is changing. Read more.
Home

Quick Start REST Service application in .NET

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Nov 23, 2012

CPOL

2 min read

viewsIcon

13944

To start coding RESTful services in .NET

Introduction

While searching on net I came across many good articles about REST, But I was explicitly searching for a very basic start up example like - "Hello world" program for RESTful service. I got an idea about it from here, but this article is also an one elaborated for me.

In this article I am just showing to quickly set up a project in visual studio 2010 for RESTful service.

Step 1

  • Open visual studio 2010

Step 2

  • Open New Project window and select online Templates button

Step 3

  • Type WCF in Search box and enter. you will see WCF REST template 40 or above. Select this template.

Step 4

Now open your chrome browser and go to here (cREST client) to install a REST client tool. Alternatively you can go to web store of chrome and search "rest client", There are many REST client tools for chrome and similarly for mozilla also there are plugins available for REST clients.

Using the Code

The boilerplate code you will get after creating a new project will include a service class and a sample Item class. Sample Item represents the data structure that we are exposing in this service. Off course we should replace this class with our own class and implement the service methods.

Initially, we get a GetCollection method as a "Hello world" method. Lets run this project now.

//
 [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class Service1
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [WebGet(UriTemplate = "")]
        public List<SampleItem> GetCollection()
        {
            // TODO: Replace the current implementation to return a collection of SampleItem instances
            return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
        }
 
        [WebInvoke(UriTemplate = "", Method = "POST")]
        public SampleItem Create(SampleItem instance)
        {
            // TODO: Add the new instance of SampleItem to the collection
            throw new NotImplementedException();
        }
 
        [WebGet(UriTemplate = "{id}")]
        public SampleItem Get(string id)
        {
            // TODO: Return the instance of SampleItem with the given id
            throw new NotImplementedException();
        }
 
        [WebInvoke(UriTemplate = "{id}", Method = "PUT")]
        public SampleItem Update(string id, SampleItem instance)
        {
            // TODO: Update the given instance of SampleItem in the collection
            throw new NotImplementedException();
        }
 
        [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public void Delete(string id)
        {
            // TODO: Remove the instance of SampleItem with the given id from the collection
            throw new NotImplementedException();
        }
 
    }

Step 5

Run the application and let the browser open with default page, since there is no web page in this application, A directory list will be showed.

Step 6

Copy the address of server from browser - http://localhost:4145/ (in my case) append the name of service class "service1" and paste this to GET text box of REST client in chrome .

Thats it, you should get a output in xml saying "Hello".