Why Web API is Not Just for MVC

Dan Carpenter December 26, 2014

In this post, Eric Butler, Senior Software Developer at Miles Technologies, discusses the value of utilizing Web API in Web Forms Application. 

It’s true that when most people speak of Web Application Programming Interface (API), they are referring to its use within an Model–view–controller (MVC) web application. While Web API naturally fits within the MVC world, it is still an Asp.Net technology, and as such, can be used in a Web Forms application.

Web API can replace those awkward page level static methods that once roamed the land of ASP.NET. Old ASMX services are extinct, and WCF is just plain overkill and obnoxious for almost everything. Let’s look at a common scenario that Web API can handle just fine.


Suppose you want to show a list of items in your database using client-side technology to make it look really snazzy. Sure, you could set up a WCF service to fetch them, but trying to get that to work client-side is a nightmare. You think maybe ASMX is a better alternative, but you realize it’s obsolete. Perhaps you should create a static page method, and set up your ScriptManager to allow page methods, and then call the auto-generated JavaScript functions that get created by the Asp.Net framework?



Instead, you could add a Web API controller to your application, set up simple routing, and then, call your API using jQuery.getJSON. Your controller will return all the data you need in JSON format for your client-side script to consume.


Setting up a Web API capable Web Forms app is easy. I’ll set up a Web Forms app using Visual Studio 2013 using .Net 4.5:


I create a new folder under the WebAPISample project and called it “Controllers.” I right click on this folder and add a new item. I select Web API from the list on the left under C# -> Web:


I call it ItemsController.cs:


For this sample, I used the RouteConfig class found in the App_Start folder, and added a few extra configurations:


Now, let’s set up a simple test. In my items controller, I am going to delete everything inside the class and add a single method:


To finish this off, I’ll update the Default.aspx page in the root directory:

Run the app and you should see the default page look something like this:

Once added to your application, you can call your API using any web technology you like, since it is simply a URL you invoke with http verbs such as GET, POST, PUT, DELETE, etc. My personal preference is jQuery AJAX because most common requests are bundled in nice functions like jQuery.post, or jQuery.getJSON.

What about security? How do I prevent unauthorized access to my Web API? I won’t get into Asp.Net Identity here, but you can easily restrict your API by adding [Authorize] attributes to the methods in your API controller or to the entire controller itself. This will prevent users, who are not authorized, from accessing your resources. You can even restrict access to methods based on user roles or user names.

Web API can offer your Web Forms application the same power that many MVC apps have – a clean, powerful API that can be consumed by any web technology and by almost any application. There’s no need to add web service references to your consuming applications. Just invoke a web request to the URL and you’ll get back some JSON!

What’s your opinion of web API?  Do you agree or disagree with anything in this blog post?  Hit us up in the comments and we’ll get back to you.

Interested in learning more about how custom software solutions can help your business run more efficiently? Contact us today to speak to a software expert.


Leave Us a Comment

Your email address will not be published. Required fields are marked *

Do great things.

Get Started