Bit of Technology

  • Archive
  • About Me
    • Advertise
    • Disclaimer
  • Speaking
  • Contact

Detailed Tutorial for Building ASP.Net Web API RESTful Service

November 25, 2013 By Taiseer Joudeh 33 Comments

When you are designing, implementing, and releasing new REST API a lot of constraints and standards should be considered; once the API is available to public, and clients start consuming it, significant changes are very hard!

There are lot of API designs in the web; but there is no widely adopted design which work for all scenarios, that is why you are left with many choices and grey areas.

So in this multi-part series we’ll be building from scratch a sample eLearning system API which follows best practices for building RESTful API using Microsoft technology stack. We’ll use Entity framework 6 (Code First) and ASP.Net Web API.

Before digging into code samples and walkthrough I would like to talk little bit about the basics and characteristics of RESTful services and ASP.Net Web API.

Basics of RESTful services:

REST stands for Representational State Transfer, it is a simple stateless architecture that runs over HTTP where each unique URL is representation of some resource. There are four basic design principles which should be followed when creating RESTful service:

  • Use HTTP methods (verbs) explicitly and in consistent way to interact with resources (Uniform Interface), i.e. to retrieve a resource use GET, to create a resource use POST, to update a resource use PUT/PATCH, and to remove a resource use DELETE. 
  • Interaction with resources is stateless; each request initiated by the client should include within the HTTP headers and body of the request all the parameters, context, and data needed by the server to generate the response.
  • Resource identification should be done through URIs, in simple words the interaction between client and resource in the server should be done using URIs only. Those URIs can act like a service discovery and interface for your RESTful service.
  • Support JSON or/and XML as the format of the data exchanged in the request/response payload or in the HTTP body.

For more information about RESTful services, you can check this information rich IBM article.

Introducing the ASP.NET Web API

The ASP.Net Web API shipped with ASP.Net MVC4, it has been around more than one year and half. It is considered a framework for building HTTP services which can be consumed by broad range of clients such as browsers, smart phones, and desktop applications. It is not considered as a part of the MVC framework, it is part of the core ASP.Net platform and can be used in MVC projects, Asp.Net WebForms, or as stand alone web service.

ASP.Net Web API Stack

ASP.Net Web API Stack

Today with the increase of using smart phones and the trend of building Single Page Apps (SPA); having a light weight Web API which exposes your services data to clients is very important. Asp.Net Web API will help you out of the box in creating RESTFul compliant services using features of HTTP like (URIs, request/response, headers, versioning, and different content formats).

What we’ll build in this multi-part series?

We need to keep things simple and easy to understand and learn from, but at the same time we need to cover different features provided by ASP.Net Web API and best practices to build RESTFul service.

We’ll be building a simple API for eLearning system, this API allows students to enroll in different courses, allows tutors to view students enrolled in each course, do CRUD operations on courses and students, and many more operations. I’ll be listing detailed use cases which we’ll covered in the next post.

We’ll discuss and implement different Web API features such as:

  • Using different routing configuration, controllers, resources association, formatting response, and filters.
  • Implementing Dependency Injection using Ninject.
  • Apply results pagination using different formatting techniques.
  • Implementing complex CRUD operations on multiple resources.
  • Securing Web API by using Basic authentication, forcing SSL.
  • Implementing API Versioning using different techniques (URL versioning, by query string, by version header, and by accept header).
  • Implement resources cashing.

Note: we’ll not build a client in this series, we’ll use Fiddler or Postman REST client to compose HTTP requests.

I broke down this series into multiple posts which I’ll be posting gradually, posts are:

  • Building the Database Model using Entity Framework Code First – Part 1.
  • Applying the Repository Pattern for the Data Access Layer – Part 2.
  • Getting started with ASP.Net Web API – Part 3.
  • Implement Model Factory, Dependency Injection and Configuring Formatters – Part 4.
  • Implement HTTP actions POST, PUT, and DELETE In Web API – Part 5.
  • Implement Resources Association – Part 6.
  • Implement Resources Pagination – Part 7.
  • Securing Web API – Part 8.
  • Preparing Web API for Versioning – Part 9.
  • Different techniques to Implement Versioning – Part 10.
  • Caching resources using CacheCow and ETag – Part 11.

Update (2014-March-5) Two new posts which cover ASP.Net Web API 2 new features:

  • ASP.NET Web API 2 Attribute Routing.
  • IHttpActionResult as new response type and CORS Support.

Update (2014-April-16) New multi part series tutorial which covers building OData Service using ASP.Net Web API.

  • Building OData Service using ASP.Net Web API.

All the source code for this series is available on GitHub, you can download it locally or you can fork it. If there is nothing clear or ambiguous please drop me a comment and I’ll do my best to reply your questions.

To get the best of this tutorial I recommend you to follow the posts one by one. Happy coding and hopefully this series will help you to get started with ASP.Net Web API 🙂

Source code is available on GitHub.

Related Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Identity 2.1 Accounts Confirmation, and Password Policy Configuration – Part 2
  • ASP.NET Identity 2.1 with ASP.NET Web API 2.2 (Accounts Management) – Part 1

Filed Under: ASP.NET, ASP.Net Web API, CodeProject, Entity Framework, Web API Tutorial Tagged With: API, ASP.NET, Entity Framework, REST, RESTful, Tutorial, Web API, Web Service

Comments

  1. Piusn says

    December 7, 2013 at 8:56 am

    Cool stuff.

    Reply
  2. Chad Miller (@FakeChadMiller) says

    December 19, 2013 at 7:14 am

    Many thanks for this tutorial!

    Reply
    • tjoudeh says

      December 19, 2013 at 8:48 am

      You welcome, glad you like it!

      Reply
  3. Ronald says

    December 19, 2013 at 10:58 pm

    Anybody who likes the to create a serie on building a modern front-end for this tutorial?

    Reply
    • tjoudeh says

      December 19, 2013 at 11:58 pm

      I’m designing a SPA using angularJS and bootstrap which will use this API as its backend, keep tuned.

      Reply
      • robforee says

        December 26, 2013 at 6:07 pm

        I’ll be looking forward to it!

        Reply
      • Yi Jiang says

        May 16, 2014 at 8:35 am

        Taiseer I have been hoping for an update on this thread from quite some time now. I am good at WebPI now, thanks to you! Any idea when is the AngularJS/Bootstrap section going to be released?

        Reply
        • Taiseer Joudeh says

          May 16, 2014 at 8:48 am

          Hello Yi, I’m happy that you liked the tutorial, for the mean time you can check my other Foursquare Explorer tutorial which uses angular, bootstrap and simpler web API project.

          Reply
  4. Gaspard says

    December 20, 2013 at 9:09 am

    This summer (2013), I had to present a dissertation (MSc) involving Near Field Communication (NFC) and your tutorial is almost similar to some features implemented in my dissertation using Java. We used android on the client side (NFC).

    Reply
  5. Shaify Mehta says

    February 19, 2014 at 4:43 pm

    Informative post. I will definitely use Web API for creating web services for Social networking website (http://itoall.com) developed my me. I was going to start on WCF Rest Service but now i will start developing the Web Api. Thanks for this great info.

    Reply
    • Taiseer Joudeh says

      February 19, 2014 at 4:46 pm

      Web API is the right way to build RESTful service. Glad you like it and good luck in your next project 🙂

      Reply
  6. Randhir says

    February 26, 2014 at 10:46 am

    I have download the eLearing.webapi but it’s not avaiable any view..please suggest me how can run without view.

    Thanks
    Randhir

    Reply
    • Taiseer Joudeh says

      February 26, 2014 at 11:49 am

      Hi Randhir,
      Can you elaborate more please? what do you mean by view? This is an API with no views/pages if this is what you meant. If you want to test it then you need to use fiddler or Postman.

      Reply
      • Randhir says

        February 26, 2014 at 1:27 pm

        Yes ,view like html pages.anyway got it.

        Thankd
        Randhir

        Reply
        • Taiseer Joudeh says

          February 26, 2014 at 1:33 pm

          Great, hope you will learn from it, let me know if you need any help!

          Reply
          • Randhir says

            February 26, 2014 at 4:15 pm

            Its so good article for learning purpose.but some view files(html) is missing under eLearning.web API..If provide me then better understanding.

            Thanks for sharing this article.

  7. GuruPrasad says

    March 16, 2014 at 3:27 am

    Thanks for the tutorial. I was new to MS technology stack but with help of resources like your I am able to build up my skills very quickly.

    I haven’t yet completed the whole series but couldn’t wait to thank you.!!

    Reply
    • Taiseer Joudeh says

      March 16, 2014 at 8:27 am

      You are welcome, glad you like it, let me know if you need further help.

      Reply
  8. Shanaya Singh says

    August 6, 2014 at 6:58 pm

    very informative. thanks.

    http://www.etechpulse.com/2014/07/net-web-api-introduction.html

    Reply
  9. Chally says

    April 3, 2015 at 7:28 pm

    Thank for the wonderful work with this tutorial. I am currently moving through the series. I just want to ask if this is all still relevan in 2015, or if there are any new developments we should keep in mind.

    Thanks again!!

    Reply
    • Taiseer Joudeh says

      April 3, 2015 at 8:53 pm

      You are welcome, this is till relevant if you are using VS 2013, but this will change slightly in VS 2015 and ASP.NET 5, but if you are new to REST APIs then this will give you good understanding on how you architect and build HTTP Service.
      Good luck!

      Reply
      • Chally says

        April 27, 2015 at 2:41 pm

        Thank you Sir for the reply. Good thing I’m using VS 2013.
        Also, can i take it that a solution modeled from this tutorial is production ready? Or are there other things I’ll need to do before i can push to production?

        Reply
        • Taiseer Joudeh says

          April 29, 2015 at 7:00 pm

          This has been for a while, but as far as I remember yes you can deploy it to production directly and all you need to do is to configure your connection string to point to production SQL instance.

          Reply
          • Chally says

            April 30, 2015 at 9:40 am

            Ok. Appreciate..

  10. Anibal Pendas says

    June 25, 2015 at 7:08 pm

    Excellent article. Thanks for sharing your knowledge..

    Anibal Pendas
    CEO and Co-Founder
    Cube and Colors Solutions, LLC

    Reply
    • Taiseer Joudeh says

      June 30, 2015 at 12:34 am

      Thanks Anibal, glad to hear this.

      Reply
  11. Muhammad Raheel Yousuf says

    January 19, 2017 at 11:04 am

    what about database first sorry i m beginner on EF

    Reply
    • Taiseer Joudeh says

      January 22, 2017 at 10:44 pm

      Hello Muhammad, DB first is easier than CF, if you have a specific question please let me know to help.

      Reply
  12. bn says

    May 8, 2022 at 8:49 pm

    Is it better to build a website in C # or VB.NET? I want to develop a website to move it from WordPress towards the closed code of asp net?

    Reply
  13. bn says

    May 8, 2022 at 8:50 pm

    Is it better to build a website in c shurp or VB.NET? I want to develop a website to move it from WordPress towards the closed code of asp net?

    Reply
  14. bn says

    May 8, 2022 at 8:50 pm

    Is it better to build a website in c shurp or VB NET? I want to develop a website to move it from WordPress towards the closed code of asp net? And how long does it take to develop a website on an average basis

    Reply

Trackbacks

  1. 70-486 – Useful Web API Tutorials | Matt DuFeu says:
    January 20, 2014 at 10:54 pm

    […] the name suggests, this is a more detailed tutorial of Web API than the other resources and although it’s well done, I wouldn’t recommend you start off with […]

    Reply
  2. seo Tips says:
    May 20, 2014 at 8:20 am

    seo Tips

    Detailed Tutorial for Building ASP.Net Web API RESTful Service – Bit of Technology

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Taiseer

Husband, Father, Consultant @ MSFT, Life Time Learner... Read More…

Buy me a coffeeBuy me a coffee

Recent Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 – Part 5
  • ASP.NET Identity 2.1 Roles Based Authorization with ASP.NET Web API – Part 4

Blog Archives

Recent Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 – Part 5
  • ASP.NET Identity 2.1 Roles Based Authorization with ASP.NET Web API – Part 4

Tags

AJAX AngularJS API API Versioning ASP.NET ASP.Net 5 Authentication Autherization Server Azure Active Directory Azure Active Directory B2C Azure AD B2C basic authentication Code First Dependency Injection Documentation Entity Framework Entity Framework 7 Facebook Foursquare API Google Authenticator Identity jQuery JSON JSON Web Tokens JWT MVC 6 Ninject OAuth OData Resource Server REST RESTful RESTful. Web API Single Page Applications SPA Swagger-ui Swashbuckle TFA Token Authentication Tutorial Two Factor Authentication Web API Web API 2 Web API Security Web Service

Search

Copyright © 2022 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in