Using WebContext
Both in sitelets and remote functions, WebSharper provides a value of type WebSharper.Web.Context that gives some contextual information about the current request.
Retrieving the context
Sitelets
In sitelets, the functions creating sitelet instances like Sitelet.New, Sitelet.Content, or Sitelet.Infer
all take a function argument of type Web.Context<'T> -> 'T -> Async<Web.Content> (Sitelet.Content is dropping the 'T value as it's for a single known value).
This means that when you implement a sitelet, you can retrieve the context by using the function argument directly. For example:
Remote functions
In remote functions, the context can be retrieved using the function WebSharper.Web.Remoting.GetContext(). Be careful to only call it from the thread from which your function was called. A typical remote function has the following structure:
User Sessions
The main reason to use the context is to manage user sessions. The member UserSession has the following members:
-
LoginUser : username: string * ?persistent: bool -> Async<unit>Logs in the user with the given username. This sets a cookie that is uniquely associated with this username. Set
persistenttotrueif the user session should last beyond the user's current browser session. -
LoginUser : username: string * duration: TimeSpan -> Async<unit>Logs in the user with the given username. This sets a cookie that is uniquely associated with this username. The user session should last for the given duration.
-
GetLoggedInUser : unit -> Async<string option>Retrieves the currently logged in user's username, or
Noneif the user is not logged in. -
Logout : unit -> unitLogs the user out.
The implementation of these functions relies on cookies and thus requires that the browser has enabled cookies.
Other Context functionality
-
ApplicationPath : stringis the virtual application path of the server. -
RootFolder : stringis the physical folder on the server machine from which the application is running. -
RequestUri : Uriis the URI of the request. -
ResolveUrl : string -> stringresolves URL paths starting with~into absolute paths prefixed with theApplicationPath. -
Environment : Dictionary<string, obj>is a host-dependent environment.-
On ASP.NET Core, this contains the following items:
- Under key
"WebSharper.AspNetCore.HttpContext", the HTTP context, of typeMicrosoft.AspNetCore.Http.HttpContext.
- Under key
-
-
Json : WebSharper.Core.Json.Provideris an instance of the JSON provider, which can be used to serialize and deserialize JSON data. -
Metadata : WebSharper.Core.Metadata.Infois the runtime metadata for the current application, which can be used to inspect the types available in the client code in the WebSharper application.
Sitelet Context
For sitelets, the object is typed with the type of the endpoint, so you get an instance of `WebSharper.Web.Context``. This provides a method for generating safe links.
Link : Endpoint -> stringgenerates a link to the given endpoint, ensuring that the link is safe and correctly formatted.