The Content module contains functions to add headers at the Content level without needing to resort to the Context, for example:

1
2
3
4
let baseSitelet =
    Sitelet.Infer <| fun ctx action ->
        Content.Text("example page")
        |> Content.WithHeader "Pragma" "no-cache"

To generalize it to the Sitelet level, you can do something like this:

1
2
3
4
5
6
7
8
module Sitelet =

    let MapContent (f: Async<Content<'T>> -> Async<Content<'T>>) (s: Sitelet<'T>) =
        { s with Controller = { Handle = s.Controller.Handle >> async.Return >> f >> Content.FromAsync } }

let mySitelet =
    baseSitelet
    |> Sitelet.MapContent (Content.WithHeader "Pragma" "no-cache")
By on 9/13/2016 5:15 AM ()

Is there some solution to the problem in C# version of WebSharper?

By on 7/3/2018 11:52 PM ()

Hmm, I'm trying to implement it but there seems to be some API missing, namely a public Content constructor from FSharpContent<object>. That would allow the following:

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
28
29
    using Microsoft.FSharp.Core;

    public static class SiteletExtensions
    {
        public static Sitelet<object> MapContent(this Sitelet<object> sitelet, Func<Task<Content>, Task<Content>> func)
        {
            var handle = FuncConvert.ToFSharpFunc((object x) =>
                Content.FromTask(func(new Content(Task.FromResult(sitelet.Controller.Handle.Invoke(x)))))
            );
            var controller = new Controller<object>(sitelet.Controller.Handle);
            return new Sitelet<object>(sitelet.Router, controller);
        }
    }

    public class MyWebsite
    {
        [Website]
        public static Sitelet<object> Main =>
            new SiteletBuilder()
                // .With ...
                .Install()
                .MapContent(x =>
                    x.WithHeaders(
                        Http.Header.Custom("Cache-Control", "no-cache, no-store, must-revalidate"),
                        Http.Header.Custom("Pragma", "no-cache"),
                        Http.Header.Custom("Expires", "0")
                    )
                );
    }

I'll open an issue to add it, and at the same time add F# and C# versions of MapContent to the library.

Edit: opened issues #980 to make the C# Content constructor public, and #981 to add MapContent to the library.

By on 7/4/2018 1:42 AM ()

Thanks, it works nicely. Even better with Content.WithHeaders

1
2
3
4
5
6
7
8
9
10
let noCacheHeaders = 
    [ 
        ("Cache-Control", "no-cache, no-store, must-revalidate");
        ("Pragma", "no-cache");
        ("Expires", "0")
    ] |> Seq.map (fun (a,b) -> Http.Header.Custom a b)

let mySitelet =
    baseSitelet
    |> Sitelet.MapContent (Content.WithHeaders noCacheHeaders)
By on 9/14/2016 1:48 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