3 simple steps to create your first ASP.NET Web API service

By | September 29, 2013
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

Introduction to ASP.NET Web API

ASP.NET Web API is a framework that simplifies the creation of HTTP services

Using ASP.NET Web API we can create HTTP services those are non-SOAP based like plain XML or JSON strings etc. with added advantages.

  • Allowing to create resource-oriented services using the full features of HTTP.
  • Exposing services to a variety of clients easily like browsers or mobile devices etc.
Before diving into details while going through this ASP.NET Web API Tutorial, I would like to clarify one misconception that ASP.NET Web API has replaced WCF. WCF is still a powerful programming model for creating SOAP based services that use a variety of transport protocols like HTTP, TCP, Named Pipes or MSMQ etc. You can find the same implementation by using WCF REST in another article i.e. “5 simple steps to create your first RESTful service“.

Apart from Visual Studio 2010 or 2012, we also need MVC 4.0 to implement this HTTP service. For the purpose of this implementation, I am going to use Visual Studio 2010.
You can download MVC 4.0 for Visual Studio 2010 from Microsoft as follows:

Following are 3 simple steps to create HTTP service that returns non-SOAP based data.

  • Create Web API Project
  • Prepare domain Model
  • Adding Controller class
You are designing an ASP.NET Web API application. You need to select an HTTP verb to allow blog administrators to moderate a comment. Which HTTP verb should you use?

  • A. GET
  • B. POST
  • C. DELETE
  • D. PUT

To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.

 Correct Answer: D

Let’s move forward step by step to create a simple HTTP service using ASP.NET Web API.

ASP.NET Web API Tutorial with Simplified Approach

1. Create Web API Project

  • Open Visual Studio and create “New Project” i.e. File -> New Project.
  • Choose “ASP.NET MVC 4 Web Application” template and name project as “FirstWebAPIService”.
  • When you click “OK” button, a new window will appear for selecting a sub-template. Actually for ASP.NET MVC 4 Web Application, we have multiple sub-options i.e. Empty, Internet Application, Web API etc.
  • Choose “Web API” and simply press “OK” button.
ASP.NET Web API Project
Web API Tutorial Template
  • A default ASP.NET MVC 4 Web API template project is created. As its an MVC application template, so you will easily find “Model”, “View” and “Controller” folders inside it.
2. Preparing domain Model
Now in a second step in this asp.net web api tutorial, we need to prepare the model.
  • Right click on the “Model” folder and choose “Class” under “Add” from the context menu as shown in figure.
  • Name the class as “Product.cs”.
Model for Web API
Product Model
Here is the code for Product class.
3. Adding Controller Class
Controller class plays an important role, because request coming from client hits the controller first. Then the controller decides which model to use to serve the incoming request. So, in order to add a controller:

  • Right click on the “Controller” folder and choose “Controller” under “Add” from the context menu as shown in figure.
  • Name the controller as “ProductsController”.
Product Controller in Web API Tutorial
First Web API Service
  • Click the “Add” button, a new controller class will be added.
More Web API Tutorial
In order to make the things simple, we will load the model with data inside this controller instead of loading it from database. You can easily find more Web API Tutorial here that explains the process to fetch the records from database. Following is the code for controller class.
Don’t forget to add “using FirstWebAPIService.Models;” at the top of the controller class.

Now, it’s time to test your HTTP service using ASP.NET MVC Web API.
Run the application by pressing “CTRL+F5”, Welcome window will appear as follows:

Output for ASP.NET Web API Service

In order to call our Product controller, change the URL as “http://localhost:XXXX/api/products”. You will see the results as shown in following output window.

Rendered Content for Web API Service
Final output returned can be displayed differently by different browsers. Here is the output of google chrome version 29.0.1547.66.
You are working as a Web API developer with WebDevTutorials. You have developed a Student HTTP Service using ASP.NET Web API technology and you want to return a View from Web API method.

  • A. Use ViewBag.
  • B. Use Partial View against Web API method.
  • C. You can’t return view from ASP.NET Web API method.
  • D. None of Above.

To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.

 Correct Answer: C

Hopefully, this simple web api tutorial will be helpful for developers to code their first HTTP service using ASP.NET MVC Web API. Further, for performing all CRUD operations using Web API, Follow the click. Soon you will be able to download this ASP.NET Web API Tutorial PDF.

ASP.NET MVC & Web API Jobs [Updated Daily]

Top ASP.NET MVC Jobs

A2614A - ASP.NET, T-SQL, MVC, C#, Javascript Developer
Source: Indeed
Details: ASP.NET MVC development using C# and Razor - Required - 4 Years. The candidate also will work on the new custom modules/web sites developed in ASP.NET MVC C#...  More
11 days ago

Harrisburg, PA 01-December-2017

Lead Developer/Solutions Architect
Source: Kestra Financial, Inc.
Details: Strong Skills & Experience in Creating Apps in ASP.NET & ASP.NET MVC; The Lead Developer/Solutions Architect for Kestra Financial will lead a development team...  More
30+ days ago

Austin, TX 07-November-2017

Full-Stack Software Engineer, ASP.NET
Source: Indeed
Details: C#, ASP.NET MVC, Entity Framework, SQL Server. Ideal candidates have demonstrable experience building highly scalable, secure applications leveraging the ASP...  More
30+ days ago

New York, NY 17-October-2017

Senior Level .NET Developer
Source: 3Cords Solutions
Details: Previous ASP.NET, MVC, C# JavaScript and JQuery experience is required. The successful candidate will have previous MVC, Web API, UI, C# and ASP.NET experience....  More
7 days ago

Nashville, TN 04-December-2017

Programmer / Developer - ASP.Net MVC SQL
Source: Capgemini
Details: Number of years with MVC -. 2+ years of hands on experience with MVC, Web API, jQuery, Responsive design, TFS....  More
20 days ago

Richmond, CA 21-November-2017

Full-stack C#/.NET Developer
Source: Pinnacle Partners
Details: 5+ years’ experience with ASP.NET, MVC and Entity Framework. Pinnacle Partners is assisting our client in their search for a full-stack C#/.NET Developer....  More
27 days ago

Indianapolis, IN 46240 14-November-2017

Full-Stack Senior Software Engineer
Source: Digital Air Strike
Details: 5+ years of object oriented design and solutions development in the .NET framework including C#, ASP.NET MVC, WebAPI, Entity Framework, and LINQ....  More
30+ days ago

Scottsdale, AZ 19-September-2017

ASP.NET Web Developer
Source: Indeed
Details: C# and ASP.NET MVC programming:. Develop new ASP.net MVC applications conforming to the design specifications provided....  More
30+ days ago

Orlando, FL 08-November-2017

Sr.Full Stack .Net Developer
Source: Indeed
Details: Experience with Web API and REST, WCF, LINQ, and MVC preferred (2+ year). Sr....  More
30+ days ago

Dallas, TX 27-October-2017

Senior Full Stack Software Engineer
Source: MaxPoint
Details: Experience with ASP.NET, MVC, C# and/or Java. Senior Full Stack Software Engineer....  More
12 days ago

Austin, TX 78759 29-November-2017

Mid level .Net Developer with SQL( US Citizenship required)
Source: Indeed
Details: 5+ years of Web development experience using ASP.net, C#, MVC version 3 or 4. Develop and maintain web applications using Microsoft .NET, ASP.NET MVC 3 or 4, C#...  More
11 days ago

Arlington, VA 30-November-2017

Associate Software Engineer
Source: Zapproved
Details: You should be excited about or have some exposure to our technology stack, which includes AWS, JavaScript, .NET, MVC frameworks such as Backbone.js, AngularJS,...  More
3 days ago

Portland, OR 97209 08-December-2017

Senior Full Stack .NET Developer
Source: Indeed
Details: Experience in ASP.NET Web Forms, MVC, WebAPI or WPF development (5+ years). To be successful the ideal candidate must have a background in ASP.NET MVC, C#, VB...  More
9 days ago

Chatsworth, CA 91311 02-December-2017

Senior .Net Developer
Source: Indeed
Details: Expertise in ASP.Net MVC 4 Framework. ONLY H1B TRANSFERS / GC EAD / GC/ TN Visas & US CITIZENS*....  More
3 days ago

Chevy Chase, MD 08-December-2017

Senior .Net Developer
Source: Indeed
Details: Create web user interface server-side functionality using ASP.NET Web Forms, ASP.NET MVC. SENIOR .NET DEVELOPER*....  More
12 days ago

Lansing, MI 29-November-2017

Senior .NET Developer
Source: Indeed
Details: 3-8 years of designing and developing web applications with C# and ASP.NET MVC 5. A cutting edge software development firm is actively seeking a .NET Developer...  More
24 days ago

Chicago, IL 17-November-2017

Senior .Net Developer
Source: Indeed
Details: Demonstrated understanding of C#, ASP.NET, MVC and Microsoft SQL Server 3. Bachelor’s degree in Computer Science or Software Engineering • 6+ years of recent...  More
30+ days ago

Frankfort, KY 19-October-2017

Senior .Net Developer
Source: Indeed
Details: Minimum 4 years experience in C#, VB, ASP.net MVC and Web Forms. Senior .Net Developer:....  More
30+ days ago

Golden, CO 80401 15-October-2017

Senior .Net Developer
Source: Indeed
Details: ASP.NET MVC 5.x, VB.NET framework experience is required. C# 5.x, PHP 5.x, ASP, SQL, T-SQL required; The Lead Engineer position will balance business solution...  More
30+ days ago

Las Vegas, NV 28-August-2017

Senior .Net Developer
Source: Indeed
Details: ASP.NET MVC – minimum 5 years. NET Web Developer III creates technical solutions for multiple, concurrent client projects with emphasis on CMS, CRM and...  More
30+ days ago

Minneapolis, MN 07-September-2017

Senior .Net Developer
Source: Indeed
Details: Develop enterprise web applications using Microsoft .NET, ASP.NET MVC 3 or 4, C#. Is looking for multi-talented web developers to add to their dynamic team...  More
30+ days ago

Washington, DC 31-August-2017

.Net Software Developer-Remote
Source: Vector-Solutions
Details: Vector Solutions is an award winning, provider of cutting edge technology based training solutions for a range of markets including Architecture, Engineering,...  More
3 days ago

Tampa, FL 33609 08-December-2017

CMS Developer with strong .NET
Source: RDA Corporation
Details: Three+ years’ development experience using C#, ASP.NET, MVC and SQL technologies. We are currently seeking a senior software engineer to join our Washington DC...  More
2 days ago

Washington, DC 09-December-2017

Sr. .NET Developer
Source: Bank of America
Details: Skilled in C#, ASP.NET MVC, WebAPI, AJAX, WebSocket. Duties may include the research and documentation of process and data flows, query design, distributed...  More
30+ days ago

Jersey City, NJ 07302 15-October-2017

Software Engineer I
Source: Vertafore
Details: Have developed either single page web applications leveraging modern frameworks (Angular, React) or more traditional multipage websites leveraging ASP.NET MVC...  More
30+ days ago

Bothell, WA 98012 21-September-2017

Top 10 Interview Questions and Answers Series:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
  • wow…seems great

  • Really great post .. thanks sharing your topic

    website

  • Thanks very much… I m waiting for CRUD operations using Web API…. Thanks again….

  • Easy to do tutorial. Thanks mate however I am having this error when I run the program:

    This XML file does not appear to have any style information associated with it. The document tree is shown below.

    No HTTP resource was found that matches the request URI ‘http://localhost:59074/api/products’.

    No type was found that matches the controller named ‘products’.

  • Dear Abraham,
    You need to check you configuration and verify the routeTemplate as follows:
    1. Open WebAPIConfig.cs file under “App_Start” folder in Visual Studio.
    2. Inside Register method, routeTemplate value should be equal to “api/{controller}/{id}”.

    As I have the following:
    config.Routes.MapHttpRoute(
    name: “DefaultApi”,
    routeTemplate: “api/{controller}/{id}”,
    defaults: new { id = RouteParameter.Optional }
    );

  • when i try this…everything was ok till chnging url bt when i change url it land at google search page …why this??

    • Dear Nilam,
      Kindly look for my above comments on Abraham query. You might need to update routeTemplate.

      If it doesn’t resolve your issue, kindly elaborate your issue a bit more.

  • This comment has been removed by a blog administrator.

  • swapna

    i am getting error as MvcApplication1.Controllers.productsController.Product’ is a ‘field’ but is used like a ‘type. Plzz help me out in this problem

    • I believe you are making some mistake while calling a method. This type of error occurs normally when someone calls a method at class level instead of making call inside a method.

  • Above are around 52 errors, If you can remove others, hopefully things will be clear but it seems you have some wrong placement of methods (outside class) etc.

  • Roman Denysiuk

    IMRAN, could you tell me the way how to allow update/delete/create records (through web service) only for authenticated users?

  • Edwin Yesid Castañeda Henao

    Imran, this blog is excellent. Conglaturations !!

  • user2083

    well explained! easy to understand for a newcomer.

    here one thing i want to mention is while executing above given example my browser was showing below error :

    “This XML file does not appear to have any style information associated with it. The document tree is shown below.

    No HTTP resource was found that matches the request URI ‘http://localhost:45788/api/products’.

    No type was found that matches the controller named ‘products’.

    And the reason i found was, in my application, ‘ProductsController’ class was by default inheriting “Controller” as base class as given below:

    “public class ProductsController : Controller”

    instead of ‘ApiController’ class as shown in above example code.

    So After adding namespace “using System.Web.Http;” in ‘ProductsController’ class and inheriting ‘ApiController’ as base class as follows:

    “public class ProductsController : ApiController”

    it worked.

    thanks again for this good piece of work.

  • user2083

    well explained! easy to understand for a newcomer.

    one thing i want to mention, while executing above given example browser was showing below error :

    “This XML file does not appear to have any style information associated with it. The document tree is shown below.

    No HTTP resource was found that matches the request URI ‘http://localhost:45788/api/products’.

    No type was found that matches the controller named ‘products’.

    And the reason i found was, in my application, ‘ProductsController’ class was by default inheriting “Controller” as base class as given below:

    “public class ProductsController : Controller”

    instead of ‘ApiController’ class as shown in above example code.

    So After adding namespace “using System.Web.Http;” in ‘ProductsController’ class and inheriting ‘ApiController’ as base class as follows:

    “public class ProductsController : ApiController”

    it worked.

    thanks again Imran for this good piece of work.