I’ve been playing with ASP.net WebAPI recently and wanted to see if it was possible to use WebAPI into an ASP.net Web Forms Application. It looks like the answer is yes that is possible.
I found a number of examples online eg “5 Simple Steps for Using Web API in ASP.NET Web Forms Application” but most .net examples these days seem to be in C# and whilst I can program in C# I normally choose VB.net as a first choice. Hence I decided to recreate Imrans example using VB.net.
For the purposes of this example I’m just using IIS Express locally on a Windows 7 PC with .net Framework 4.5.1 and Visual Studio 2013 Update 4, but you could easily use IIS on Windows Server if you prefer.
1. Create a new application in Visual Studio 2013
- Open Visual Studio a choose File -> New Project
- Give the project a name such as “WebAPIExample” and choose “Templates > Visual Basic > Web > ASP.NET Web Application”
- Next choose an Empty project type and check “Web Forms” and “Web API” for the core references:
- This should give you a basic web application with very little additional clutter:
2. Add a Model
- Add a Class item called “Car.vb” to the Models folder as follows:
This is the code for the Car Model ( Models/Car.vb )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Public Class Car Private _Id As Integer Private _Make As String Private _Model As String Public Property Id() As Integer Get Return _Id End Get Set(value As Integer) _Id = value End Set End Property Public Property Make() As String Get Return _Make End Get Set(value As String) _Make = value End Set End Property Public Property Model() As String Get Return _Model End Get Set(value As String) _Model = value End Set End Property End Class |
Public Class Car Private _Id As Integer Private _Make As String Private _Model As String Public Property Id() As Integer Get Return _Id End Get Set(value As Integer) _Id = value End Set End Property Public Property Make() As String Get Return _Make End Get Set(value As String) _Make = value End Set End Property Public Property Model() As String Get Return _Model End Get Set(value As String) _Model = value End Set End Property End Class
3. Add a Controller
- Add a Class item called “CarsController.vb” to the Controllers folder as follows:
Note: Don’t choose “Controller..” file type above… just choose “Class” for this simple example:
This is the code for the CarsController.vb Controller ( Controllers/CarsController.vb )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Imports System.Net Imports System.Web.Http Public Class CarsController Inherits ApiController Private cars As Car() = New Car() _ {New Car() With { _ .Id = 1, _ .Make = "Aston Martin", _ .Model = "V8 Vantage" _ }, New Car() With { _ .Id = 2, _ .Make = "BMW", _ .Model = "Z8" _ }, New Car() With { _ .Id = 3, _ .Make = "Ferrari", _ .Model = "California" _ }, New Car() With { _ .Id = 4, _ .Make = "Pagani", _ .Model = "Zonda" _ }} Public Function GetCars() As IEnumerable(Of Car) Return cars End Function End Class |
Imports System.Net Imports System.Web.Http Public Class CarsController Inherits ApiController Private cars As Car() = New Car() _ {New Car() With { _ .Id = 1, _ .Make = "Aston Martin", _ .Model = "V8 Vantage" _ }, New Car() With { _ .Id = 2, _ .Make = "BMW", _ .Model = "Z8" _ }, New Car() With { _ .Id = 3, _ .Make = "Ferrari", _ .Model = "California" _ }, New Car() With { _ .Id = 4, _ .Make = "Pagani", _ .Model = "Zonda" _ }} Public Function GetCars() As IEnumerable(Of Car) Return cars End Function End Class
4. Test in a web browser:
- Run the Project and navigate to http://website:PORT/api/cars – you should see the following:
5. Some of the “Magic” behind the scenes
- In step 1 when we created the project we checked “Web API” which added the core references and folders for Web API:
- Behind the scenes Visual Studio created “/App_Start/WebApiConfig.vb” which contains the routing information, which takes care of mapping incoming requests for api/{controller}/{id} to the relevant Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web.Http Public Module WebApiConfig Public Sub Register(ByVal config As HttpConfiguration) ' Web API configuration and services ' Web API routes config.MapHttpAttributeRoutes() config.Routes.MapHttpRoute( name:="DefaultApi", routeTemplate:="api/{controller}/{id}", defaults:=New With {.id = RouteParameter.Optional} ) End Sub End Module |
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web.Http Public Module WebApiConfig Public Sub Register(ByVal config As HttpConfiguration) ' Web API configuration and services ' Web API routes config.MapHttpAttributeRoutes() config.Routes.MapHttpRoute( name:="DefaultApi", routeTemplate:="api/{controller}/{id}", defaults:=New With {.id = RouteParameter.Optional} ) End Sub End Module
- We can then look at the “/Global.asax” file in the web root folder to see how the Register method of the WebApiConfig Module is then bound to fire upon the global Application_Start event:
1 2 3 4 5 6 7 8 9 10 | Imports System.Web.Http Public Class Global_asax Inherits HttpApplication Sub Application_Start(sender As Object, e As EventArgs) ' Fires when the application is started GlobalConfiguration.Configure(AddressOf WebApiConfig.Register) End Sub End Class |
Imports System.Web.Http Public Class Global_asax Inherits HttpApplication Sub Application_Start(sender As Object, e As EventArgs) ' Fires when the application is started GlobalConfiguration.Configure(AddressOf WebApiConfig.Register) End Sub End Class
To get a full understanding of what is going on I’d recommend looking at the Web.config file also.
That’s is for now. I hope this is helpful to some VB.net developers. In the next few instalments I’ll explore the other REST methods, PUT, POST and DELETE.
Updated: Part 2 is available here
Mark,
Thanks so much for this tutorial. I’ve been looking far and wide. I landed mainly upon C# and MVC examples that I tried to convert unsuccessfully to VB. I needed the VB for consistency with shop culture. In conjunction with a clear, step by step method, you called to light the “Magic” behind the scenes and pointed out the Web API checkbox. That small detail turned the final corner. So, as they say, you always find what you’re seeking in the last place you look. After a lot of looking, you had the complete, straightforward answer. I only wish I had found it sooner. Thanks again.
PS: Great photo of New Zealand!
Art
You are very welcome – I’m glad it helped 🙂