5 simple steps for using Web API with ASP.NET Web Forms Application

By | May 12, 2014
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

I have seen this question more than once while going through different ASP.NET forums that “Is it possible to use Web API with ASP.NET Web Forms Application?” Simple answer to the question is “YES, we can“.

Let’s discuss about it in more details. As we already know that Microsoft released ASP.NET MVC 4 with lots of new and exciting features including ASP.NET Web API Tutorial which is basically a framework for building HTTP services that reaches broader range of clients including browsers as well as mobile devices. But it’s flexible enough to be used with ASP.NET Web Forms applications. In this ASP.NET Tutorial, we are using a step by step approach for using Web API with ASP.NET Web Forms. For a good comparison of features released in each version of ASP.NET MVC, click here.

ASP.NET Web Forms and Web API Tutorial

I have already implemented an HTTP service using ASP.NET Web API in one of my previous post that you can refer here. Adding a Model and a Controller is same but the difference is that we are adding it to ASP.NET Web Forms Application. So, let follow the steps as below:

  1. Create a New Web Forms Application.
  2. Add Model to Web Forms Application.
  3. Add Controller to Application.
  4. Add Routing Info to Global.asax.
  5. Making a Client Call.
For the purpose of implementation, I am using Visual Studio Express 2013 for Web here.

1. Create a New Web Forms Application

  • Open Visual Studio and create “New Project” i.e. File -> New Project.
  • Choose “ASP.NET Web Application” template and name project as “WebAPIwithWebForms”.
  • When you click “OK” button, a new window will appear for selecting a sub-template.
    Note: You may not get window for sub-template in older versions of Visual Studio.
  • Choose “Web Forms” and click “OK”.
Using Web API with Web Forms
  • An ASP.NET Web Forms template project is created.

2. Add Model

Now we need to add a new class that acts as a Model in our application as follows:
ASP.NET Web API Model
Web API Model
Following is the code for Student Model

ASP.NET Web API 2 Hands On
Building RESTful Web Service using ASP.NET Web API2 taking from beginner level and learn how to use the new features of Web API2 i.e. Attribute Routing etc. More Details

3. Add Controller class

Controller class has special importance because request coming from client reaches the controller first. Then the controller decides which model to use in order to serve the incoming request.
Right click on the project and choose “Web API Controller” under “Add” from the context menu as shown in figure.
ASP.NET Web API Controller
Web API Controller
Name the controller as “StudentsController” and press “OK” button.
For the purpose of simplicity, I’ll load the model with data inside our “StudentsController” instead of loading directly from database. Following is the code for StudentsController inheriting from ApiController class.

4. Add Routing Info to Global.asax

In order to route incoming request directly to our StudentsController, we need to add Route Information to Application_Start method of Global.asax as given below:

Now, if you run your application and try to access the following URL, you will get a JSON response of student data from api controller. Try to access the following URL and see:

     http://localhost:xxxx/api/Students

Note: Don’t forget to add “using System.Web.Http” in Global.asax.cs file.

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

5. Make a Client Call

As you have already see above your ASP.NET Web API returning data in JSON format. Now, let’s call it from your Web Form using jQuery and display it on your Web Form page.
Create a new Web Form page “WebFormClient” and add the following code to call your ASP.NET Web API Controller i.e. “StudentsController” using jQuery.Main content area will contain the following HTML for rendering Students data.

using jQuery, make a call to our controller and fetch data as follows:
// GET ALL


Hopefully, above step by step approach will be helpful in building Web API with ASP.NET Web Forms Application.

Other Related Web Development Articles:

Top 10 Interview Questions and Answers Series:

Latest Developers Jobs

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
1 day ago

Stratford, CT 23-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
3 days ago

Pasadena, CA 20-March-2017

ASP.NET Developer, 100% remote
Source: Indeed
Details: Core development tools are C#, ASP.NET 4 & 4.5, ASP.NET MVC 5, Web-form knowledgeable, NUnit, CSS, HTML, Entity Framework 6, SQL Store Procedures, jQuery, SQL...  More
30+ days ago

Remote 23-January-2017

Senior-Level Software Application Developer
Source: Indeed
Details: This is some of the most exciting .Net development going on including interesting ingredients such as C#, MVC, Angular, Azure automation, SSO, and more....  More
7 days ago

Nashville, TN 37203 17-March-2017

Mid to Senior level Demandware Developer
Source: createch
Details: Experience with web frameworks that implement MVC design pattern (Spring, Struts, Stripes, ASP.NET MVC, etc.). Mid to Senior level Demandware Developer....  More
7 days ago

San Francisco, CA 16-March-2017

Senior Full Stack Developer
Source: Indeed
Details: *Job Title: * Senior Full Stack Developer *Job Summary* * Under the general supervision of the Site Director, the Senior Full Stack Developer will join a  More
10 days ago

Modesto, CA 13-March-2017

Senior Software Engineer
Source: Indeed
Details: Web based user interface software development using Angular2, JavaScript, Bootstrap, Web API, ASP.NET MVC, etc....  More
30+ days ago

Tucson, AZ 09-February-2017

Senior C#/.NET Full Stack Developer
Source: Graphic Products
Details: 3+ years demonstrated experience in ASP.NET MVC 4/5+. We are looking for a Senior Full Stack Developer responsible for the ongoing development and advancement...  More
14 days ago

Beaverton, OR 97008 09-March-2017

Senior .Net Developer
Source: Indeed
Details: Minimum 5 years of hands-on experience with web application development utilizing ASP.NET, MVC, and JavaScript libraries such as JQuery....  More
8 hours ago

Nashville, TN 23-March-2017

IT ASP.Net Developer
Source: UnitedHealth Group
Details: This systems are custom built using an ASP.Net MVC (Bootstrap and/or Angular JS) frontend with C#.Net middle/backend layers....  More
15 days ago

Tampa, FL 33646 08-March-2017

Senior Software Engineer
Source: Guidant Financial
Details: C#, ASP.Net MVC, (PHP is a plus). Guidant Financial is disrupting the entrepreneurial journey by building tech solutions that increase the number of people who...  More
30+ days ago

Seattle, WA 05-January-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
9 days ago

Redmond, WA 15-March-2017

Developer - Full Stack ASP.NET MVC, C#, SQL Server
Source: Indeed
Details: ASP.NET MVC, C#, SQL Full Stack Server Developer (Austin, Tx)*. Who We Are ...*....  More
30+ days ago

Austin, TX 78759 17-February-2017

Senior Software Engineer
Source: Indeed
Details: Advanced Coding skills in C#, JavaScript, EXT JS, AngularJS, JSON / XML, ASP.NET, MVC, REST, Web Services, Windows Services....  More
1 day ago

Harrisburg, PA 22-March-2017

Senior Software 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
30+ days ago

Stratford, CT 17-January-2017

Sr, Full Stack Developer
Source: Indeed
Details: Minimum 4 years experience with writing WebAPI/RESTFul API and working in an ASP.NET MVC environment. Keyventer is seeking a *Sr.*....  More
24 days ago

Seattle, WA 27-February-2017

Full Stack Web Developer
Source: Indeed
Details: Back End design and development, utilizing C#, .NET Framework, ASP.NET MVC, and ASP.NET WebAPI, to create new end-to-end web experiences and revise existing...  More
8 days ago

Redmond, WA 15-March-2017

.NET Application Developer
Source: SS&C Technologies
Details: ​Design and develop with ASP.Net, MVC, C#. ABOUT SS&C TECHNOLOGIES:....  More
30+ days ago

Sandy Hook, CT 07-February-2017

Senior Software Engineer, .NET
Source: Indeed
Details: AngularJS, ASP.NET MVC, jQuery, C#, SQL and OO principles, design patterns and RDBMS. Ideal candidates have demonstrable experience building highly scalable,...  More
7 days ago

New York, NY 16-March-2017

Senior Software Engineer
Source: Indeed
Details: C#.NET, ASP.NET MVC & WebAPI. ICombat is seeking a Senior Software Engineer in Waukesha, WI....  More
7 days ago

Waukesha, WI 16-March-2017

Sr. Front End eCommerce Developer
Source: Indeed
Details: Advanced C#, ASP.NET MVC , NodeJS, AngularJS, experience implementing/consuming REST services*. Ashley Furniture Industries, Inc....  More
9 days ago

Tampa, FL 14-March-2017

Full Stack Web Developer/Designer (No C2C)
Source: Indeed
Details: Change the flow when needed in the middle tier (usually C# Controller, ASP.Net MVC); Back End design and development, utilizing C#, .NET Framework, ASP.NET MVC,...  More
8 days ago

Bellevue, WA 15-March-2017

MK-10474 Senior Software Engineer -C#/ASP.NET MVC/JavaScript
Source: JobServe USA
Details: Full Life Cycle Web Application Development - ASP.NET MVC and C#. This role requires candidates with previous experience developing enterprise applications with...  More
1 day ago

San Mateo, CA 22-March-2017

MK-10473 Senior Software Engineer - C#/ASP.NET MVC/JavaScript
Source: JobServe USA
Details: Full Life Cycle Web Application Development - ASP.NET MVC and C#. This role requires candidates with previous experience developing enterprise applications with...  More
1 day ago

Tempe, AZ 22-March-2017

Senior Web Engineer
Source: Indeed
Details: Experience building web applications with C# Or Java, ASP.NET MVC. 3+ years Senior Web application development experience building large scale, highly...  More
9 days ago

Boca Raton, FL 14-March-2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET Web API ASP.NET Web Forms 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.