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

.Net Support Engineer
Source: Indeed
Details: Good knowledge of ASP DOT NET with C Sharp. .NET / Angular JS or KnockOut JS / ASP.NET MVC / C#.NET / SQL Server / Web API. .Net Support Engineer....  More
1 day ago

New York, NY 21-June-2017

Software Developer (.NET)
Source: Advantech
Details: Back-end coding skills using C#, ASP.NET MVC, Web API, MS-SQL / T-SQL. Responsibilities include design, new development, testing and maintaining MVC - based web...  More
29 days ago

Carol Stream, IL 60188 25-May-2017

C# .Net Developer (Locals Only)
Source: Indeed
Details: 3.AngularJS, ASP.NET MVC, JQuery, HTML5. 1.Good in C# and Microsoft .Net....  More
8 hours ago

Redmond, WA 22-June-2017

Software Engineer. Net
Source: Indeed
Details: JQuery, SSRS, Design Pattern, ASP.NET MVC, and UML, Microsoft Test Manager. C#, ASP.NET with MVC, SQL Database 2012/2014, and Business Analysis, Report Services...  More
1 day ago

Boca Raton, FL 33487 21-June-2017

.NET Software Engineer
Source: Indeed
Details: 5+ years experience in C#, Asp .Net, MVC. O MVC, MVVM, pub-sub pattern, factory pattern, IOC and client server systems....  More
2 days ago

Atlanta, GA 20-June-2017

.NET Developer
Source: Skills Provision Ltd
Details: O ASP.NET MVC 4+. O Telerik UI for ASP.Net MVC. Are you ready for your next challenge?...  More
30+ days ago

Houston, TX 04-May-2017

.Net Software Engineer
Source: Indeed
Details: Strong experience in developing Web Applications using C#.Net, ADO.NET/Asp.Net MVC, jquery, JavaScript, CSS, WEB APIs, SQL Server....  More
22 days ago

Sunnyvale, CA 31-May-2017

C# / .NET Developer
Source: Veterans Sourcing Group
Details: Web Development with JavaScript and ASP.NET MVC. C# / .NET Developer....  More
1 day ago

Jersey City, NJ 07310 22-June-2017

C# .Net Developer
Source: Indeed
Details: Net 4.5+, C#, SQL Server 2012, ASP.Net MVC, Visual Studio 2015. We are looking for c# full stack developer....  More
2 days ago

Fremont, CA 20-June-2017

Senior .Net Developer
Source: Indeed
Details: MVC:. The candidate also will work on the new custom modules/web sites developed in ASP.NET MVC C# platform. Job description (short):....  More
16 hours ago

Harrisburg, PA 22-June-2017

.NET/C#/JavaScript Web Programmer
Source: Indeed
Details: ASP.NET MVC Framework, especially MVC 4. INET is currently hiring detail oriented programmers to join our web development team....  More
1 day ago

Waukesha, WI 21-June-2017

C#/.Net/MVC Developer
Source: RevolutionInsure
Details: If you are a seasoned full stack .NET MVC developer interested in a startup. Experience developing within a Windows environment using Microsoft’s Visual Studio...  More
9 days ago

Austin, TX 13-June-2017

C# / .Net Developer
Source: Rumba Info Solutions
Details: Two+ (2) years HTML5, ASP.NET MVC, JavaScript, CSS, jQuery, Angular, Telerik, JSON, XML. C# / .Net Developer will guide, design and develop our next generation...  More
16 days ago

Detroit, MI 06-June-2017

C# .Net Developer
Source: Indeed
Details: Object-oriented analysis and design, .NET (C#, ADO.NET, Entity framework, ASP.NET MVC, LINQ, WinForms), SQL Server, XML, HTML, JavaScript, JQuery, CSS, MVC...  More
17 days ago

Malvern, PA 05-June-2017

Software / Support Engineer - Relocate OK
Source: PetDesk
Details: Experience developing web applications built on ASP.NET MVC. We’re PetDesk and we work in the best industry in the world....  More
5 days ago

San Diego, CA 18-June-2017

Mid-Level .Net Developer
Source: Centare
Details: We are currently looking to add a .Net Developer to our consulting team in Madison. This person will be responsible for contributing to local clients'  More
30+ days ago

Madison, WI 10-April-2017

C# .Net Developer
Source: Indeed
Details: Expertise in ASP.NET MVC 4 Framework · Working experience on HTML5 and CSS3 · Good understanding of .Net 4.5 · Experience working with REST and SOAP Web...  More
20 days ago

Washington, DC 02-June-2017

C# .Net Developer
Source: Indeed
Details: Web development using ASP.Net MVC, HTML5, CSS, JavaScript and JQuery. C# .NET DEVELOPER*....  More
21 days ago

Naperville, IL 01-June-2017

Entry or Junior C# .Net Developer
Source: Indeed
Details: Requirements: • BS degree in computer science, software engineering, or closely related field. • 1+ years of experience in software development. •  More
21 days ago

City Industry, CA 01-June-2017

Lead .Net Developer
Source: Indeed
Details: .NET MVC, Entity Framework, SQL, Front end, ASP.Net, C#*. Strong expertise with C#, ASP.Net MVC, Entity Framework, OOPS Concepts, Web API and WCF services....  More
6 hours ago

Orlando, FL 22-June-2017

ASP.Net C# Developer
Source: Career Evolutions
Details: Classic ASP (for conversion of legacy applications). 5+ years of ASP.NET, MVC and C# experience. Career Evolutions is looking for an ASP.Net C# Developer for a...  More
22 days ago

Scottsdale, AZ 85261 31-May-2017

.NET Software Engineer
Source: Indeed
Details: You have strong programming skills with .NET (C#) and related frameworks (ASP.NET, MVC, WebAPI, etc.). Gavant builds custom software to innovate and solve...  More
30+ days ago

Troy, NY 12180 04-May-2017

APPLICATION SYSTEMS PROGRAMMER II
Source: The State of Florida
Details: Visual Studio 2013/2015, TFS 2013, ASP.Net MVC, Single Page Applications, and Web Services. Highway Safety and Motor Vehicle....  More
7 days ago

Tallahassee, FL 32399 15-June-2017

Dot Net Developer
Source: Indeed
Details: ASP.NET MVC, JQuery, Entity framework, HTML programming. Develop enterprise web or desktop applications using *Microsoft .NET, ASP.NET, C#, application...  More
15 days ago

Redmond, WA 07-June-2017

Lead Developer
Source: Indeed
Details: XML, XSL, CSS, HTML, ASP/ASP.NET, MVC, JavaScript, Entity Framework, GIT as well as SQL Server, and other relational databases....  More
7 days ago

Cleveland, OH 15-June-2017

Top 10 Interview Questions and Answers Series:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET Web API Tags: , ,

About Web Development

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

  • 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.