Exploring the Online Templates: Creating a Web API with F# and ASP.NET
F# 3.0 is included as part of Visual Studio 2012 and comes with a gallery of community contributed online templates for F#.
Time to explore!
One online template is "F#/C# MVC 4" using ASP.NET Web API. Let's use this template to create a HTTP REST Web API which responds to requests as follows:
GET http://myhost.com/api/values --> the JSON value ["value1","value2"]
GET http://myhost.com/api/values/5 --> the JSON value 5
POST http://myhost.com/api/values --> does nothing by default, but you can adjust to write to a backend service
PUT http://myhost.com/api/values/5 --> again, does nothing by default, but you can adjust to write to a backend service
DELETE http://myhost.com/api/values/5 --> again, does nothing by default, but you can adjust to write to a backend service
To start, create a New Project in Visual Studio 2012 and choose the Online section of the New Project dialog, as shown below.
Next, choose the "F# C# MVC 4" template. For me, this is the first template on the list.
Give the solution project a name, then create the solution. This will download and install the project template. Accept the MS-PL license conditions, and then choose the "WebAPI" project kind. If desired, include a project for tests.
The first, "...WebApi" is a standard C# Web site project to act as a service API host for your service code (which is written in F#). You can apply all standard C# ASP.NET Web API programming techniques here.
The second, "...WebAppApi" is your F# implementation of your Web API.
The third, "...WebAppTests" contains tests for your Web API.
You can now run your solution by hitting F5, deploying to localhost. This starts the C# Web site project:
You can now use HTTP requests directly to access the data returned by the Web API. For example, try accessing http://.../api/values directly from your browser
This will give a download of either JSON or XML text. If JSON it will contain the data:
This data is derived from the F# code for implementing the "ValueController" controller which governs everything returned by http://.../api/values and any subordinate requests such as http://.../api/values/5. These permissible HTTP requests form the WebAPI you have implemented.
The F# code that implements the various HTTP actions is in ValueController.fs and is as follows:
type ValuesController() =
// GET /api/values
member x.Get() = [| "value1"; "value2" |] |> Array.toSeq
// GET /api/values/5
member x.Get (id:int) = "value"
// POST /api/values
member x.Post ([<FromBody>] value:string) = ()
// PUT /api/values/5
member x.Put (id:int) ([<FromBody>] value:string) = ()
// DELETE /api/values/5
member x.Delete (id:int) = ()
This F# type has a specific form that is expected by the ASP.NET Web API. The mappinng is somewhat obvious: the Get methods implement the HTTP GET actions, the GET method with a parameter implements the api request .../values/5 where an integer parameter is given, and so forth. In the case of Get(), the return values are presented as a sequence (IEnumerable) of strings, which is automatically marshalled to XML or JSON.
This shows how simple it is to get started with an HTTP Web API implementation in F#. The start page for the ASP.NET application you have created gives further links to documentation on the ASP.NET Web API which you can use to get started with writing more detailed controllers. Most the documentation will be in C#, but it is simple to translate the documentation on controllers into F#.
If you're interested in contributing to more samples of using ASP.NET and F# together, the F# MVP Ryan Riley is working on a Web API Koans project, and working on porting the ASP.NET MVC4 samples (see http://aspnetwebstack.codeplex.com and http://aspnet.codeplex.com ). One place to discuss things with Ryan is on the email lists of the FSharpx project.
There are endless possibilities for what you can do from here such as
- Use F# functional programming to control the complexity of information-shovelling, database access and analytical computations in the server-side of your web application
- Use F# asynchronous programming for efficient management of server-side computation
- Use F# 3.0 type providers and other data-access capabilities to implement data-rich server-side functionality
- Use F# 3.0 NuGET integration to access the huge range of NuGET packages that are available (one example: Math.NET for statistics and matrix computations)
- Using the F# interoperability with C# to design an ASP.NET website that accompanies your service
- Deploy the Web API to Azure
Thanks to Dan Mohl for this great template.
The F# Team