Simple .NET web development with F #

I am looking for HTML output to a webpage using F #. Any ideas?

+7
source share
2 answers

To give a specific "HOWTO", I tried to create a simple ASP.NET MVC application that implements key parts in F #. As noted in the answer provided in the link, the best way to use F # for web development is to create a C # web application and move all types that implement the actual behavior to the F # library.

Although it is possible to create a direct F # web project, there are many limitations (for example, not perfect intellisense and possibly other problems), so managing aspx files in C # is probably the best 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 several pages. I will ignore the page that processes the accounts (you can delete it).
  • In Controllers there is a HomeController.cs file that contains the functionality for the main page (loading data, etc.). You can delete the file - we reimplement it in F #.
  • Add a new F # Library project and add links to ASP.NET MVC assemblies. There are many of them (see the C # Project), but most importantly, you will need System.Web.Mvc.dll and the others referenced by this (the F # compiler will tell you which ones you need).
  • Now add the code below Module1.fs - this implements the original HomeController that was used to write in C #.

The source code is as follows:

 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 just a reimplementation of the C # source code (creating one class). I used the original C # namespace so that the MVC structure can easily find it.

The Views\Home\Index.aspx from the C # project defines the user interface and uses the data that you installed in the ViewData dictionary from your F # project.

This "HOWTO" shows how to use F # to write an ASP.NET MVC application, but the steps for creating a regular WebForms application will be essentially the same - create a C # web application and move the implementation classes to the F # library, which will reference C # web application (in fact it will not contain much C # code).

+10
source share

If someone is interested in a more detailed discussion, I wrote an article on this topic:

This article shows a relatively simple MVC web application created in F #. The controllers and the model are implemented in a separate F # library (along with the route display). The sample also demonstrates how to use LINQ from F # to access data (including some neat tricks, such as composing quotes).

+9
source share

All Articles