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

Junior .NET Developer
Source: Publishers Clearing House
Details: Windows Forms, WCF, ASP.NET, MVC. Publishers Clearing House (PCH), headquartered in Port Washington, NY (soon to be Jericho, NY!)....  More
14 days ago

Port Washington, NY 11050 12-April-2017

Senior .Net Developer
Source: Indeed
Details: Excellent server side development in ASP.NET, MVC, ADO.NET, LINQ, Web API, Windows Service and. We are looking for a strong, well rounded, self-motivated web...  More
4 days ago

Kingwood, TX 22-April-2017

Software Engineer C#/ASP.NET MVC/JavaScript
Source: Cormant, Inc.
Details: You will be working on full stack development including Marionette client, and a C# MVC architecture with NHibernate/RDBMS as the back-end....  More
5 days ago

San Luis Obispo, CA 21-April-2017

.NET Software Engineer
Source: CCC Information Services Inc.
Details: Sr. .NET Developer Cerritos, CA CCC Information Services, Inc. CCC provides the technology, data and insight needed to help our customers make informed,  More
5 days ago

Cerritos, CA 21-April-2017

Junior .Net Developer
Source: MassMutual Financial Group
Details: Experience working with WPF, CSS, Classic ASP, Infragistics controls, and SharePoint a plus, but not required. Junior .Net Developer....  More
20 days ago

Charlotte, NC 06-April-2017

Senior .Net Developer (Full-Stack)
Source: Indeed
Details: 4+ years of C# .NET and ASP.NET MVC development. Design, develop, and deliver web application components for a distributed system....  More
8 days ago

Austin, TX 18-April-2017

.Net Develoer
Source: conrep
Details: Experience with ASP.NET MVC and Bootstrap. The candidate also will work on the new custom modules/web sites developed in ASP.NET MVC C# platform....  More
12 days ago

Harrisburg, PA 17112 13-April-2017

Senior.Net Web Developer
Source: Indeed
Details: MVC. Our client – an established yet growing eCommerce firm seeks an accomplished .Net developer to fill a key role in the development of public-facing web...  More
6 days ago

Springfield, PA 19-April-2017

Senior .NET Developer
Source: Indeed
Details: *U.S. Citizens and those authorized to work in the U.S. are encouraged to apply. We are unable to sponsor at this time.* *Job Description: Senior .NET  More
14 days ago

Bloomfield, MI 12-April-2017

Junior to Mid-level .Net Developer
Source: Indeed
Details: Experience with the MVC pattern for web development. US Citizens or Greencard that do not require a Visa Sponsorhip for Full-Time Employment*....  More
7 days ago

Englewood, CO 18-April-2017

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

Baton Rouge, LA 23-March-2017

.NET Software Engineer (Front End or Back End)
Source: Indeed
Details: Full stack Engineers will also get to work on our Front End within a ASP.NET MVC + JavaScript environment....  More
9 hours ago

San Diego, CA 92122 26-April-2017

Junior .NET Web Developer
Source: Indeed
Details: 1+ years experience ASP.NET MVC. The Award Zone provides tools for organizations to provide highly-effective motivational programs, such as employee reward and...  More
14 days ago

Atlanta North, GA 11-April-2017

Senior Software Developer .NET ASP.NET MVC
Source: Indeed
Details: Build new applications with .Net (C#) & ASP.NET MVC – heavy focus on client web portal. Reporting to the Vice President, Technology, the Senior Software...  More
25 days ago

New York, NY 10006 31-March-2017

Sr. C#/ASP.NET Developer
Source: Logic20/20
Details: *Strong C#, .NET framework (ASP.NET MVC, Entity Framework), jQuery, HTML 5 development Qualifications. Logic20/20 is a Seattle based Management & Technology...  More
8 days ago

Seattle, WA 98115 18-April-2017

Software Developer
Source: Indeed
Details: Experienced in ASP.NET MVC 5, CSS3, HTML5, JavaScript, JQuery, Knockout.js and AngularJs. This would be a managed service position:....  More
9 days ago

Seattle, WA 16-April-2017

.NET Software Engineer
Source: Shipbob
Details: We have opportunities to solve our clients needs with a wide variety of frameworks and platforms such as ASP.NET MVC, Azure SQL Server, Angular and many more....  More
20 days ago

Chicago, IL 06-April-2017

Software Development Engineer (.NET)
Source: Indeed
Details: Talentry is looking for a solid .net developer (c#, WCF, MVC, angular) with good communication skills who likes the idea of working in an environment that...  More
22 days ago

Irvine, CA 03-April-2017

Full stack .Net Mobile Application Developer
Source: Posh Technologies
Details: Visual Studio 2013+, C#, ASP.NET MVC 5 and Web API 2. 5+ years of working experience with C#, ASP.Net MVC, SQL Server, JavaScript....  More
5 days ago

Redmond, WA 21-April-2017

Software Engineer, Jr.
Source: Indeed
Details: HTML, HTML5, MVC, JavaScript, CSS, XML, SQL Server 2008/2014, Restful web services, AJAX, Testing Automation and OOD....  More
26 days ago

Phoenix, AZ 30-March-2017

Sitecore Developer
Source: Ursus Incorporated
Details: Solid experience with C#, ASP.NET, MVC, JavaScript, HTML5, CSS3, Razor. About the Role:....  More
19 hours ago

Sacramento, CA 95834 25-April-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
30+ days ago

Beaverton, OR 97008 09-March-2017

Junior .NET Developer (no 3rd party candidates)
Source: Indeed
Details: Candidate must be clearable for a federal government public trust clearance; no third party candidates. *Responsible for developing and supporting .NET/C#  More
30+ days ago

Herndon, VA 16-March-2017

Front End Developer
Source: Indeed
Details: Microsoft ASP.NET MVC experience. Design, develop, and deliver front end web application components for a distributed system....  More
4 days ago

Annapolis, MD 21401 21-April-2017

.NET/C# Software Integration Engineer
Source: BlueVolt
Details: Solid programming experience with C#, WCF, Restful services using WEB API, and ASP.NET MVC. BlueVolt is growing....  More
29 days ago

Portland, OR 27-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 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.