I'm looking to output some HTML to a web page using F#. Any ideas?
-
Have a look at this question: http://stackoverflow.com/questions/1135959/f-and-asp-net – Tamas Czinege May 02 '10 at 20:01
-
1If you're asking regarding ASP.NET (I think so, based on the tags) then it's probably a duplicate. However, there are other ways for generating HTML from F# (e.g. directly, but that's probably not what you're after). – Tomas Petricek May 02 '10 at 20:13
-
1All the related questions that I've looked at say that it's possible but lack any detail in explaining how you actually do it. – Matthew H May 02 '10 at 20:27
2 Answers
To give some specific "HOWTO", I tried creating a simple ASP.NET MVC application that implements the key parts in F#. As noted in the referenced answer, the best way to use F# for web development is to create a C# web application and move all the types that implement the actual behavior to an F# library.
Although it may be possible to create directly F# web project, there are many limitations (e.g. not perfect intellisense and possibly other issues), so managing the aspx
files in C# is probably a better idea.
Here is what I did (using Visual Studio 2010):
- Create a new C# project "ASP.NET MVC 2 Web Application". This creates a new C# project with some initial template and a few pages. I'll ignore the page that handles accounts (you can delete it).
- There is
HomeController.cs
file inControllers
, which contains functionality for the homepage (loading of data etc.) You can delete the file - we'll re-implement it in F#. - Add new "F# Library" project and add references to ASP.NET MVC assemblies. There is a plenty of them (see the C# project), but most importantly, you'll need
System.Web.Mvc.dll
and others that are referenced by this one (the F# compiler will tell you which ones you need). - Now, add the code below to
Module1.fs
- this implements the originalHomeController
, which used to be written in C#.
The source code looks like this:
namespace MvcApplication1.Controllers
open System
open System.Web.Mvc
[<HandleError>]
type HomeController() =
inherit Controller()
member x.Index() =
x.ViewData.["Message"] <- "Welcome from F#"
x.View() :> ActionResult
member x.About() : ActionResult =
x.View() :> ActionResult
This is simply a re-implementation of the original C# code (creating a single class). I used the original C# namespace, so that the MVC framework can easily find it.
The file Views\Home\Index.aspx
from the C# project defines the user-interface and uses the data that you set to the ViewData
dictionary from your F# project.
This "HOWTO" shows how to use F# to write ASP.NET MVC application, but the steps to create an ordinary WebForms application would be essentially the same - create a C# web application and move the implementing classes to F# library, which will be referenced from the C# web application (which won't actually contain much C# code).

- 240,744
- 19
- 378
- 553
-
Thank you! This is just what I was after! =D One thing: I noticed that this works: "member x.Index() = x.View()" Note the lack of type annotations and casting. Is this ok? It works fine, but I'm curious as to why you did yours differently. – Matthew H May 02 '10 at 21:59
-
@Matt: If it works, then I suppose it's Ok :-). I think that MVC uses Reflection internally, so it is probably a bit flexible. I did my version differently, because I simply translated the C# version (exactly as it was, to avoid possible problems with various details). – Tomas Petricek May 02 '10 at 22:17
In case somebody is interested in a more detailed discussion, I wrote an article about this topic:
The article shows a relatively simple MVC web application created in F#. Controllers and model are implemented in a separate F# library (together with the route mapping). The sample also demonstrates how to use LINQ from F# for data access (including some neat trick such as composition of quotations).
- ASP.NET and F# (I.) - Creating MVC web applications in F#
- Visual Studio 2010 Template - Adds MVC Web Application to "New Project"

- 240,744
- 19
- 378
- 553