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 – Full Stack
Source: Indeed
Details: 5-7 years of experience with the Microsoft .NET UI development stack (ASP.NET MVC & WCF). At Upfront, we make every visit valuable for patients and providers....  More
2 days ago

Chicago, IL 13-December-2017

Full-Stack Software Engineer, ASP.NET
Source: Indeed
Details: C#, ASP.NET MVC, Entity Framework, SQL Server. Ideal candidates have demonstrable experience building highly scalable, secure applications leveraging the ASP...  More
30+ days ago

New York, NY 17-October-2017

C# Software Engineer
Source: Indeed
Details: 1+ years of experience with web development with ASP.Net MVC and JavaScript. CRISP - The State of Maryland Health Information Exchange is a regional health...  More
30+ days ago

Columbia, MD 13-September-2017

Software Development Engineer
Source: Indeed
Details: Experience implementing a RESTful API using ASP.NET MVC 4. We are seeking a talented Software Engineer to contribute to the development and maintenance of an...  More
30+ days ago

Des Moines, IA 06-November-2017

Software Development Engineer II - 4071
Source: Indeed
Details: Experience with ASP.NET MVC (especially Razor views and HTML helper extensions). ISoftStone North America is seeking a Software Development Engineer II to join...  More
4 days ago

Redmond, WA 12-December-2017

Software Engineer
Source: Indeed
Details: You will use your technical expertise and problem solving skills to develop a wide variety of operational grade software solutions (i.e., ASP .NET MVC web...  More
10 days ago

Herndon, VA 05-December-2017

Software Engineer
Source: Indeed
Details: Proficiency with ASP.NET MVC, C#, and SQL. Is the leading Digital Health technology company servicing the needs of the emergency and public safety industries....  More
24 days ago

Broomfield, CO 80021 21-November-2017

Software Engineer
Source: Indeed
Details: C#, ASP.NET MVC and/or Java. This position will exercise the applicant’s skills as a software engineer in both the commercial and government space....  More
30+ days ago

Johnstown, PA 15904 19-October-2017

Software Engineer
Source: Indeed
Details: NET, ASP.NET MVC and Razor Syntax. About the Role*....  More
30+ days ago

Columbus, OH 19-October-2017

Software Engineer
Source: Indeed
Details: API first design and SPA applications with Angular, Web API and ASP.NET MVC. NCARB’s Information Systems (IS) Directorate has an opening for a Software Engineer...  More
30+ days ago

Washington, DC 20006 13-October-2017

Software Engineer
Source: Indeed
Details: _Experience with Microsoft .NET C# and ASP.NET, MVC, & Entity Framework_. Intellicheck Mobilisa is a publicly-traded technology company located in Jericho, NY...  More
30+ days ago

Long Island, NY 05-September-2017

Senior .NET Consultant
Source: Indeed
Details: EPE Innovations is a software development and consulting company, with a primary focus on Complex Data Modeling, Data Analytics and Business Intelligence...  More
7 hours ago

Houston, TX 15-December-2017

Senior .NET Consultant
Source: Indeed
Details: ASP .Net MVC.:. Strong knowledge of our core technology stack including n-tiered .Net development, TFS, MVC C# .NET, .NET Entity Framework, WCF web services, MS...  More
1 day ago

Albany, NY 14-December-2017

ASP.NET MVC Web Developer
Source: Indeed
Details: Strong or expert knowledge of C#, JQuery, JavaScript, Knockout JS, ASP.NET MVC, MSSQL. Perform in a key role in the development and enhancement of enterprise...  More
14 days ago

Evanston, IL 01-December-2017

Associate Software Engineer
Source: Indeed
Details: Experience with .NET Framework (ASP.NET, MVC, WinForms). American Healthtech, a growing software technology company, is searching for an *Associate Software...  More
7 days ago

Ridgeland, MS 39157 08-December-2017

Software Engineer Associate
Source: Indeed
Details: Familiarity with Angular, Entity Framework and ASP.NET MVC. 30% Application Development*....  More
15 days ago

Dallas, TX 30-November-2017

Software Engineer I
Source: Indeed
Details: Knowledge and experience with .NET MVC and web-based development. .NET 4 or greater, ASP.NET MVC 4 or greater, javascript frameworks such as JQuery....  More
7 days ago

Ridgeland, MS 39157 08-December-2017

ASP.NET Web Developer
Source: Indeed
Details: C# and ASP.NET MVC programming:. Develop new ASP.net MVC applications conforming to the design specifications provided....  More
30+ days ago

Orlando, FL 08-November-2017

.Net Developer (C#, ASP.Net)
Source: Indeed
Details: 2+ years' experience with ASP.NET MVC, at least 1 year using the 4-4.5 framework. This individual will assist with the development and maintenance of detailed...  More
18 days ago

Nashville, TN 27-November-2017

.Net Developer (C#, ASP.Net)
Source: Indeed
Details: 2+ years' experience with ASP.NET MVC, at least 1 year using the 4-4.5 framework. Proficient working with the following - .NET, C#/VB, SSMS, Object Oriented...  More
18 days ago

Cleveland, OH 27-November-2017

Sr. Software Engineer
Source: Indeed
Details: Experience with ASP.NET MVC. Looking for a highly skilled software engineer to provide technical leadership on its critical communication software suite....  More
8 days ago

Eden Prairie, MN 07-December-2017

Sr. Software Engineer
Source: Indeed
Details: Experience with ASP.NET MVC. Client is looking for a highly skilled software engineer to provide technical leadership on its critical communication software...  More
30 days ago

Eden Prairie, MN 15-November-2017

Sr. Software Engineer
Source: Indeed
Details: Extensive Experience with ASP.NET MVC using C#.NET. We are looking for a Sr....  More
30+ days ago

San Jose, CA 01-November-2017

Sr. Software Engineer
Source: Indeed
Details: Full stack development experience using C#, ASP.Net, MVC, WCF, REST APIs, SQL Server, JavaScript Frameworks, HTML5, CSS3,. Local Candidates Preferred....  More
30+ days ago

Burbank, CA 22-September-2017

Senior .NET Programmer/Analyst - 5+ Yrs Experience - Philadelphia, PA
Source: Marshall Dennehey Warner Coleman & Goggin, P.C.
Details: A minimum of 5 years experience in application design and development using a variety of .NET technologies and languages, including C#, VB.NET, Javascript,...  More
30+ days ago

Philadelphia, PA 26-October-2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET Web API 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.