Hello to my first post of 2014. A new year with new opportunities – so people say.
Web Services
There are three main technologies which .Net has to supply web services.
- WebService built in a .asmx file. I usually just use these for AJAX as part of a web application. There are limitation to this technology which makes it difficult to use by 3rd parties. Mainly that the return result is XML and not JSON and that it only accepts calls by POST.
- WCF, this is a very flexible and powerful technology but it is also extremely complex and not really conducive to building a simple service.
- WebAPI, a resonably simple technology based on the MVC pattern (but without the View) part.
In this blog entry I will be provding a small example of a service using WebAPI
a WebAPI Project
There are many WebAPI tutorials available on the web, like this one at MSDN. Where things get interesting is when you have multiple APIs. Then you have to do some dickering around.
For example in my project I had a API which accepted data from a Meraki device and also requests from 3rd parties for weekly update statistical data.
In this case the routing must be added in manually in global.asax
Imports System.Web.SessionState Imports System.Web.Http Imports System.Web.Routing Public Class Global_asax Inherits System.Web.HttpApplication Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application is started RouteTable.Routes.MapHttpRoute(name:="RetailerAPI", routeTemplate:="api/{controller}/{username}/{password}", defaults:=New With {.username = RouteParameter.Optional}) RouteTable.Routes.MapHttpRoute(name:="MyAPI", routeTemplate:="api/{controller}/{id}", defaults:=New With {.id = System.Web.Http.RouteParameter.Optional}) End Sub End Class
In this scenario we can support two APIs which can be directed to the appropriate controller. It can accept URI of:
/api/Meraki
/api/SalesData/username/pwd
Hopefully this little tidbit of news helped you out.