Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET
Tip/Trick

Consuming WebAPI Using jQuery

Rate me:
Please Sign up or sign in to vote.
2.67/5 (3 votes)
4 Sep 2012CPOL1 min read 26.9K   10   3

Introduction

ASP.NET Web API is a framework for building and consuming HTTP services that can be reached by a broad range of clients including browsers and mobile devices. In this sample I will show how you can build web APIs that support browser clients and can be easily called using jQuery.

Requirement

  • Visual Studio 2010 SP1, Visual Web Developer 2010 SP1 or Visual Studio 2012
  • ASP.NET MVC4

Getting Started

The following simple steps show you how to create a WebAPI and call that WebAPI using a JSON object.

Step 1

Open Visual Studio 2010 and under Web select “ASP.NET MVC4 Web Application” and name it “HelloWebAPI” and click OK.

HelloWebAPI.bmp

Step 2

After clicking the OK button the New “ASP.NET MVC4 Project” window will open, in that select “WebAPI” and click OK.

WebAPI.bmp

Step 3

To create a new Model Class, go-to Solution Explorer (F4), right click on Model->Add->Class and name the class “Product” and paste the following code.

ModelClass.bmp

Step 4

To create a new controller right-click on the Controller -> Add -> Controller and the “Add Controller” dialog will open, name the Controller “ProductsController”, then in Scaffolding options template, select “Empty API Controller” and click the Add button.

Controller.bmp

Step 5

In “ProductsController.cs”, paste the following code:

ProductController.bmp

Step 6

In Solution Explorer, expand the Views folder and open Index.cshtml, replace with the following code.

IndexPage.bmp

Step 7

Click on Start debugging (F5), the following result will show in the browser.

Output.bmp

License

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



Comments and Discussions

 
PraiseGreat blog !!! :) Pin
Member 1238506310-Mar-16 23:40
Member 1238506310-Mar-16 23:40 
GeneralMy vote of 3 Pin
HariPrasad katakam8-Jul-13 3:32
HariPrasad katakam8-Jul-13 3:32 
GeneralMy vote of 1 Pin
Dmitry Gonchar2-Mar-13 17:34
Dmitry Gonchar2-Mar-13 17:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.