Click here to Skip to main content
15,891,423 members
Articles / Web Development / ASP.NET
Tip/Trick

Quick Start REST Service application in .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Nov 2012CPOL2 min read 13.8K   10  
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

Image 1

Step 3

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

Image 2

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.

Image 3

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.

C#
//
 [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.

Image 4

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 .

Image 5

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hi, I am software developer from Delhi, India. I work in .Net and related technologies.

Personally I like technology and playing with different new features & possibilities.

Comments and Discussions

 
-- There are no messages in this forum --