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

.Net Developer
Source: Indeed
Details: ASP.NET MVC, JQuery, Entity framework, HTML programming. .Net MVC 4.0 and Strong experience in designing and developing layered web based applications with...  More
24 days ago

Bellevue, WA 01-February-2017

.NET DEVELOPER
Source: Indeed
Details: ASP.NET MVC, JQuery, Entity framework, HTML programming. .Net MVC 4.0 and Strong experience in designing and developing layered web based applications with...  More
24 days ago

Seattle, WA 01-February-2017

.Net Developer
Source: Indeed
Details: C#.NET, VB.NET and/or ASP.NET MVC, TFS.Web development tools including HTML5, JavaScript, jQuery, AJAX, LINQ, CSS,....  More
21 days ago

Rockwell, MD 03-February-2017

.NET Developer, Junior
Source: Booz Allen Hamilton
Details: 1+ years of experience with ASP.NET, MVC, or Web forms development. Booz Allen Hamilton has been at the forefront of strategy and technology for more than 100...  More
15 hours ago

Atlanta, GA 25-February-2017

Senior Software Developer #17-043
Source: Indeed
Details: Experience building web applications using Google Web Toolkit (GWT) or frameworks such as ASP.NET MVC, Spring MVC, Angular, Ember, or React....  More
3 days ago

Arlington, VA 22-February-2017

.NET / Mobile Application Developer, Junior
Source: Booz Allen Hamilton
Details: 1+ years of experience with ASP.NET, MVC, or Web forms development. Booz Allen Hamilton has been at the forefront of strategy and technology for more than 100...  More
2 days ago

Atlanta, GA 22-February-2017

Application Developer -- C#, ASP.NET Web Forms/MVC
Source: Indeed
Details: .NET framework (3.5 or newer) -- especially with .NET Web Services, ASP.NET MVC, ASP.NET Web Forms and LINQ. We have a fulltime programmer position open for an...  More
2 days ago

Golden Valley, MN 23-February-2017

Software Developer (.NET Web Services & API)
Source: Indeed
Details: C#, ASP.NET, MVC or better required, Java/Node.js a plus. If you’re looking for a rewarding career with a dynamic and fast-growth healthcare software company,...  More
3 days ago

Tucson, AZ 22-February-2017

Junior Level .NET Developer
Source: Levi, Ray & Shoup, Inc.
Details: Time to get to work! LRS Consulting Services is seeking a Junior Level .NET Developer for a 3-6 month contract-to-hire opportunity with our client in Central  More
8 days ago

Illinois 17-February-2017

.NET Software Developer
Source: Indeed
Details: ASP.NET, Entity Framework, MVC, Ajax, jQuery, Bootstrap. Web application development using Visual Studio (2013, 2015) C# and ASP.NET MVC, JavaScript, Ajax, LINQ...  More
3 days ago

Fairfax, VA 22030 21-February-2017

Junior .NET Developer
Source: SBG Technology Solutions
Details: Must have a clear understanding of ASP.NET MVC, and MVC application design pattern experience. At least five (5) years of development experience, including 4+...  More
11 days ago

Alexandria, VA 22314 14-February-2017

.Net Developer
Source: Indeed
Details: MVC:. Develop web, web services and back-office applications using the newest Microsoft stack of technologies including Net CORE, ASP.NET, MVC, Angular,...  More
7 days ago

Charlotte, NC 17-February-2017

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

Tucson, AZ 09-February-2017

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

Madison, WI 15-February-2017

Front End Developer
Source: Indeed
Details: Experience with ASP.NET MVC, Web APIs, C#, and the .NET Framework. Rational is a full service, integrated agency with proven ability to deliver across digital,...  More
1 day ago

Redmond, WA 23-February-2017

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

Port Washington, NY 11050 12-December-2016

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
7 days ago

Austin, TX 78759 17-February-2017

NET DEVELOPER - SENIOR (FULL-TIME CONTRACTUAL)
Source: State of Maryland
Details: ASP.NET MVC with Razor. SQL Server 2012, Visual Studio 2015, .NET 4.6 and MVC 5. Development, maintenance, trouble-shooting and debugging of .NET applications...  More
7 days ago

Baltimore, MD 17-February-2017

NET DEVELOPER - JUNIOR (FULL-TIME CONTRACTUAL)
Source: State of Maryland
Details: ASP.NET MVC with Razor. SQL Server 2012, Visual Studio 2015, .NET 4.6 and MVC 5. Development, maintenance, trouble-shooting and debugging of .NET applications...  More
7 days ago

Baltimore, MD 17-February-2017

.Net Developer
Source: Indeed
Details: ASP.NET MVC and WebForms. Net Developer- Las Vegas, NV. Must be authorized to work in the USA.*....  More
30+ days ago

Las Vegas, NV 06-December-2016

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

Seattle, WA 22-February-2017

Full Stack Web Developer
Source: Indeed
Details: C# / ASP.NET MVC. Opportunity to play with a modern Microsoft-oriented web stack (C# 7, Typescript, ASP.NET MVC, EF/LINQ, React/Redux, HTML5, Sass, VS2017,...  More
3 days ago

Seattle, WA 22-February-2017

.Net Developer
Source: Indeed
Details: *Job Functions and Responsibilities: * Looking for a multi-talented software/web developer to add to ITCON Services dynamic team supporting a highly visible  More
30+ days ago

Washington, DC 02-November-2016

AWS Developer
Source: Indeed
Details: Amazon experience is amust *Ruby-on-rails* and *Java* for a Amazon’s new project,. Requirement is simply three: * JavaScript: 3-5 years * Ruby on rails -...  More
8 days ago

Redmond, WA 16-February-2017

Web Developer - 3888
Source: Indeed
Details: 3-5+ years development with Microsoft ASP.NET MVC (including jQuery, Knockout/Angular). ISoftStone North America is looking for a Web Developer to join our...  More
10 days ago

Redmond, WA 14-February-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.