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

Software Engineer I
Source: Vertafore
Details: Have developed either single page web applications leveraging modern frameworks (Angular, React) or more traditional multipage websites leveraging ASP.NET MVC...  More
27 days ago

Bothell, WA 98012 21-September-2017

Junior/Mid Level Software Developer
Source: Indeed
Details: ASP.NET MVC (C#). Software developers are the engine behind the design, development, installation, testing and maintenance of software systems....  More
13 hours ago

Kennesaw, GA 17-October-2017

Software Developer
Source: Mosaic Personnel
Details: Strong background in PHP, ASP.Net Web forms, ASP.NET MVC, C#. Why You'll Love This Software Developer Opportunity....  More
1 day ago

Oklahoma City, OK 17-October-2017

Applications Software Engineer - Web Developer Sr
Source: Indeed
Details: 5+ years' programming experience in C#, .NET framework, VB, MVC, EF and WCF. Experience with Web Development ASP.NET, ASP.NET, MVC, HTML5, CSS, JavaScript, AJAX...  More
15 hours ago

Anaheim, CA 17-October-2017

Software Developer
Source: Indeed
Details: Create ASP.NET MVC controllers to access databases. Knowledge of ASP.NET MVC is preferred but not required. Our development environment runs on the Microsoft...  More
4 days ago

Orlando, FL 13-October-2017

Software Developer
Source: SDHR Consulting
Details: .NET Stack experience (C#, ASP.NET MVC, MS SQL Server). Do you have a passion for writing code?...  More
5 days ago

San Diego, CA 13-October-2017

Software Developer / Software Engineer
Source: Natoli Engineering
Details: ASP.net MVC preferred. Software Developer / Software Engineer....  More
6 days ago

Saint Charles, MO 63304 11-October-2017

Software Developer
Source: Evergreen Enterprises
Details: ASP.Net, MVC development skills using MS Visual Studio; About Evergreen Enterprises....  More
6 days ago

Richmond, VA 23225 11-October-2017

Software Developer
Source: Reynolds and Reynolds
Details: Knowledge of .NET framework including C#, ASP.NET, MVC, JavaScript, JQuery, HTML, CSS, web services, XML and SQL....  More
9 days ago

Draper, UT 09-October-2017

Software Developer
Source: Indeed
Details: Knowledge in Visual Studio 2005-2013 and .NET Framework 2.0 - 4.5.1, ASP.NET MVC 5.0, VB 6.0/.NET, C, C++, C#, Java, Tomcat, Transact-SQL, HTML, XML, JavaScript...  More
12 days ago

San Juan, PR 05-October-2017

Software Developer - Medical Billing
Source: SyMed Corporation
Details: Experience with ASP.NET MVC 5, LINQ, Entity Framework 6. Located in the beautiful Napa Valley, SyMed Corporation is a comprehensive medical billing and...  More
13 days ago

Napa, CA 94558 05-October-2017

Software Application Developer
Source: Indeed
Details: Successful candidates will likely have at least 5 years of experience in a professional capacity and a Bachelor’s degree in Computer Science, Information...  More
13 days ago

Los Angeles, CA 04-October-2017

Sr Software Developer
Source: Indeed
Details: C#, ASP.NET MVC. System redesign includes Car Buying, Retail and Scrap operations (using Mobile, MVC Web, WPF, Xamerin tablet interfaces)....  More
13 days ago

Atlanta, GA 30360 04-October-2017

Software Developer - Mid Level
Source: MartinFederal Consulting
Details: ASP.NET MVC and Web Forms. MartinFederal is seeking a self-motivated candidate for our Mid-Level Software Developer position in support of our DoD customer at...  More
16 days ago

Fort Meade, MD 20755 01-October-2017

Software Developer
Source: Gagemaker LP
Details: Intermediate to advanced skills in ASP.NET MVC , ASP.NET Web API. Software Developer Position Summary:....  More
19 days ago

San Antonio, TX 78216 28-September-2017

Software Developer ; Front-end
Source: Indeed
Details: If you are passionate about technology and enjoy creating simple solutions to complex requirements -- come join us! We are looking for topnotch developers...  More
20 days ago

San Diego, CA 27-September-2017

Software Developer 3
Source: Almac Group, Inc
Details: Minimum 2 years of experience developing applications utilizing ASP.net MVC and C#. 3 or more years of C# and ASP.net MVC experience working in a regulated...  More
27 days ago

Durham, NC 27704 20-September-2017

Software Developer
Source: Indeed
Details: Expertise should include HTML5, AngularJS, JavaScript, ASP.NET MVC and CSS. A solid understanding of working with MVC JavaScript frameworks such as Angular....  More
28 days ago

Carlsbad, CA 20-September-2017

Software Developer
Source: Indeed
Details: ASP.NET MVC, Web API. NET Framework 4.0 and above. Software Developer (.NET)*....  More
28 days ago

San Diego, CA 19-September-2017

Software Developer
Source: Indeed
Details: Experience with ASP.NET MVC and Web API. Client Resources Inc....  More
29 days ago

Lincoln, NE 18-September-2017

Software Developer
Source: Indeed
Details: 3+ years of experience with C# and ASP.NET MVC. The Software Developer will assist with new and ongoing software development as a member of an agile team...  More
22 days ago

Kennesaw, GA 25-September-2017

Software Developer 2
Source: Almac Group, Inc
Details: Minimum 1 year of experience developing applications utilizing ASP.net MVC and C#. Almac is an exceptional, award winning, drug development solutions provider...  More
29 days ago

Audubon, PA 19403 18-September-2017

Software Developer 2
Source: Almac Group, Inc
Details: Minimum 1 year of experience developing applications utilizing ASP.net MVC and C#. Almac is an exceptional, award winning, drug development solutions provider...  More
30+ days ago

Lansdale, PA 19446 14-September-2017

Software Engineer/Developer
Source: ITR (Information Technology Resources)
Details: Candidates demonstrating applied experience with ASP.NET MVC, Java, and Linux will be given extra consideration....  More
30+ days ago

Oak Ridge, TN 07-September-2017

Software Engineer/Developer
Source: ITR (Information Technology Resources)
Details: The qualified candidate will be responsible for development and coding Web and desktop applications in the .NET Framework, SQL Server, and ASP.NET MVC....  More
30+ days ago

Knoxville, TN 04-September-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.