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

Senior Software Quality Assurance Engineer
Source: OneStream Software
Details: C#, Visual Basic, SQL Server, Web Services, WCF, WPF, Silverlight, XAML, XML, ASP.NET, MVC, HTML5, JavaScript, and CSS....  More
2 days ago

Stratford, CT 23-March-2017

Sr/Lead Software Engineer – C#/.Net/SQL Server – Web Services
Source: Indeed
Details: 5+ years’ experience in server technologies such as .Net, ASP.Net, ASP.Net MVC, C#, Node.js, ASP 2.0., LDAP, Active Directory.*....  More
4 days ago

Boston, MA 20-March-2017

Senior Dot Net Developer
Source: Indeed
Details: Involved in SDLC process using the ASP.NET MVC architecture. \*\*We are not looking for students enrolled/just completed education in USA....  More
10 days ago

Redmond, WA 15-March-2017

Full Stack Web Developer (ASP.NET MVC, C#)
Source: Indeed
Details: *Title: Full Stack Web Developer* *Location: Redmond, WA* *Duration: 6 months (will extend)* *Description: * Full stack front end web developer with...  More
15 days ago

Redmond, WA 10-March-2017

Junior .NET Developer
Source: NIC
Details: MVC. In-depth experience with C#, ASP.NET and MVC. Junior .Net Developer Position....  More
1 day ago

Baton Rouge, LA 23-March-2017

SQL Server DBA/Developer
Source: Indeed
Details: Ability to code in C#.NET/VB.NET and Exposure to ASP.NET, MVC, IIS, SharePoint. Long-Term/Full-time....  More
8 days ago

Latham, NY 16-March-2017

Developer - Web - Jr
Source: Kaiser Permanente
Details: C#, Angular JS, Bootstrap, Asp.net MVC, Web API, Entity Framework. This is highly responsible work maintaining and maturing web presence, both Internet and...  More
4 days ago

Pasadena, CA 20-March-2017

.Net Web Developer/Software Engineer
Source: Proctor Financial, INC.
Details: Strong ability to work in Asp.Net, with ability to work in both MVC and WebForms. Our Developers work alongside with Quality Assurance, Business Analysts and...  More
1 hour ago

Troy, MI 25-March-2017

JR .Net Developer (GC/USC/EAD)
Source: Indeed
Details: *Position Description: * * This is highly responsible work maintaining and maturing web presence, both Internet and Intranet. Will be responsible for creation  More
11 hours ago

Pasadena, CA 24-March-2017

.NET Developer
Source: Indeed
Details: _ ASP.NET, MVC, WebAPI, jQuery, AngularJS. The role is responsible for collaborating with team members to design, code and maintain client-facing web...  More
13 hours ago

Addison, TX 24-March-2017

.NET backbone.js/Marionette.js Developer in Cleveland, OH
Source: Indeed
Details: Position: .NET backbone.js/Marionette.js Developer Location: Cleveland, OH Duration: 6-12 Months Client : Hexaware Technologies *Job Description: * *  More
16 hours ago

Cleveland, OH 24-March-2017

Sr. .Net Developer
Source: Indeed
Details: HTTP, AJAX, HTML5, ASP.NET MVC, JavaScript/jQuery and CSS3. Do you want to use your .Net and Software Development skills to indirectly help conquer cancer and...  More
17 hours ago

Alexandria, VA 24-March-2017

Full Stack .Net Developer (USC OR GC) Local only.
Source: Indeed
Details: 4+ years of experience with ASP.Net MVC. Bachelor’s degree preferably in Comp Sci or a related engineering degree....  More
17 hours ago

Houston, TX 24-March-2017

.Net Developer
Source: Indeed
Details: Experience with C#, .NET, Entity, MVC, Web API, HTML, CSS, JavaScript/jQuery, SQL. 2+ years of experience with C# (especially ASP.NET MVC, Web API, and Entity)....  More
20 hours ago

Louisville, KY 24-March-2017

Certified Microsoft .NET Web Developer
Source: Indeed
Details: The project includes .NET MVC web applications with heavy use of JavaScript, jQuery and Kendo UI components....  More
21 hours ago

Madison, WI 24-March-2017

Net Developer
Source: Indeed
Details: Advanced knowledge in building and deploying .NET based web applications using ASP.NET, ASP.NET MVC, WCF, and WebAPI. Hi,....  More
21 hours ago

Charlotte, NC 24-March-2017

.NET Developer
Source: Indeed
Details: 5 or more years building Web application using ASP.NET MVC. My client, an internationally renowned, full-service media agency located Downtown Chicago, is...  More
30+ days ago

Chicago, IL 07-February-2017

Certified .Net Developer
Source: Indeed
Details: The project includes .NET MVC web applications with heavy use of JavaScript, jQuery and Kendo UI components....  More
1 day ago

Madison, WI 23-March-2017

.Net Developer
Source: Resort Hospitality Solutions
Details: Create and maintain web applications based on a Microsoft web stack including C#, jQuery, Azure, ASP.Net MVC and ASP.Net Web Forms....  More
1 day ago

Asheville, NC 23-March-2017

Full Stack .NET Developer
Source: Forte Group
Details: Experience with ASP.NET, MVC and .NET Framework; One of the largest technology providers is currently looking for a Full Stack .NET Developer to join their team...  More
1 day ago

Chicago, IL 60606 23-March-2017

.Net Developer
Source: Resort Hospitality Solutions
Details: Create and maintain web applications based on a Microsoft web stack including C#, jQuery, Azure, ASP.Net MVC and ASP.Net Web Forms....  More
1 day ago

Duluth, GA 23-March-2017

Certified Microsoft .NET web Developer II (Job ID: WISCJP00011321)
Source: Indeed
Details: Project Duties include requirements analysis, design, front-end web application development, testing, code reviews, implementation and mentoring a team in the...  More
1 day ago

Madison, WI 23-March-2017

Full stack ASP.NET Developer
Source: Indeed
Details: MVC:. Proficiency in ASP.NET MVC 5 and C#. Our client has immediate full-time position available for skilled Dull Stack ASP.NET MVC 5/C# Web Application...  More
2 days ago

Somerset, NJ 22-March-2017

.NET Developer
Source: Procentrix
Details: .NET, C#, C++, ASP.NET, Ajax, jQuery, MVC, , WCF, Sybase SQL Anywhere/SQL Server programming. Developing custom services, components, or modules using ASP.NET...  More
2 days ago

Washington, DC 22-March-2017

Sr .Net Full stack Developer
Source: Indeed
Details: Involved in SDLC process using the ASP.NET MVC architecture. Are you passionate self-driven job seeker who can a lead in making world class software at a full...  More
23 days ago

Seattle, WA 02-March-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 IMRAN ABDUL GHANI

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.