5 Simple Steps for Using Web API in ASP.NET Web Forms Application

I have seen this question more than once while going through different ASP.NET web 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, Microsoft released ASP.NET MVC 4 with lots of new and exciting features including ASP.NET Web API 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.

I have already implemented an HTTP service using ASP.NET Web API in one of my previous posts that you can refer to. 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:

Create a New Web Forms Application

Add Model to Web Forms Application

Add Controller to Application

Add Routing Info to Global.asax

Making a Client Call

For the purpose of implementation, I am using Visual Studio Express 2013 for Web here.

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.

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.

Share

About the Author

Imran Abdul Ghani has more than 10 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 WCF Tutorials, Web Development, SharePoint for Dummies.

i have read whole article and follows the steps as well, But when come to step 4 & 5 having issue.
Problem is that i am not able to get how to access methods inside contoller and shows result accordingly in webform.