Click here to Skip to main content
13,006,288 members (57,662 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 23 Nov 2012

Quick Start REST Service application in .NET

, 23 Nov 2012
Rate this:
Please Sign up or sign in to vote.
To start coding RESTful services in .NET


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.

    [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".


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


About the Author

varun bansal
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.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 23 Nov 2012
Article Copyright 2012 by varun bansal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid