Hi,

To handle a REST interface in a sitelet, the best is to write a custom sitelet router and use the active patterns defined in UrlHelpers. For example:

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
open IntelliFactory.WebSharper.Sitelets
open IntelliFactory.WebSharper.Sitelets.UrlHelpers

type Action =
    | GetComment of id: int
    | PostComment of id: int * username: string * content: string

// This sitelet handles the url "/comments/1234" with GET and POST methods.
let MyRestSitelet =
    {
        Router =
            let route req =
                match req with
                | GET (query, SPLIT_BY '/' ["comments"; INT i]) ->
                    Some (GetComment i)
                | POST (query, SPLIT_BY '/' ["comments"; INT i]) ->
                    let query = Map.ofList query
                    Some (PostComment (i, query.["username"], query.["content"]))
                | _ -> None
            let link _ = None // You're probably not going to use context.Link to point to your API
            Router.New route link
        Controller =
            { Handle = function
                | GetComment id -> Content.NotFound // Handle this...
                | PostComment (id, username, content) -> Content.NotFound // Handle this...
            }
    }

Note that query above uses the &-separated HTML form field standard, read from the URL for GET and from the body of the request for other methods. If you wish to use eg. JSON instead, we currently don't have facilities for that but we are discussing adding them in an upcoming version of WebSharper. Meanwhile you can use standard JSON libraries such as System.Web.Script.Serialization or Newtonsoft.Json to read from req.Body.

By on 2/5/2015 7:40 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper