Skip to content

Razor view engine http handlers for Giraffe web applications.

License

Notifications You must be signed in to change notification settings

christianfolie/Giraffe.Razor

 
 

Repository files navigation

Giraffe.Razor

Giraffe

Razor view engine support for the Giraffe web framework.

NuGet Info

Windows and Linux Builds

Windows Build status

Windows Build history

Table of contents

Documentation

The Giraffe.Razor NuGet package adds additional HttpHandler functions to render Razor views in Giraffe.

In order to use the Razor functionality in an (ASP.NET Core) Giraffe application you'll need to register additional dependencies through the AddRazorEngine extension method during application start-up:

open Giraffe
open Giraffe.Razor

type Startup() =
    member __.ConfigureServices (svc : IServiceCollection,
                                 env : IHostingEnvironment) =
        let viewsFolderPath =
            Path.Combine(env.ContentRootPath, "Views")
        svc.AddRazorEngine viewsFolderPath |> ignore

If your all of your Razor views are kept in a Razor class library, then you do not need to specify a views folder path when registering the Razor dependencies. In this case there is an overload of AddRazorEngine which takes no arguments:

open Giraffe
open Giraffe.Razor

type Startup() =
    member __.ConfigureServices (svc : IServiceCollection,
                                 env : IHostingEnvironment) =
        svc.AddRazorEngine() |> ignore

razorView

The razorView http handler utilises the official ASP.NET Core MVC Razor view engine to compile a view into a HTML page and sets the body of the HttpResponse object. It requires the content type, the view name, an optional view model, an optional view data dictionary, and an optional model state dictionary as input parameters.

Example:

Use the razorView function:

open Giraffe
open Giraffe.Razor

[<CLIMutable>]
type TestModel =
    {
        WelcomeText : string
    }

let model = { WelcomeText = "Hello, World" }

let app =
    choose [
        // Assuming there is a view called "Index.cshtml"
        route  "/" >=> razorView "text/html" "Index" (Some model) None None
    ]

razorHtmlView

The razorHtmlView http handler is the same as the razorView handler except that it automatically sets the response's content type to text/html; charset=utf-8.

Example:

Use the razorView function:

open Giraffe
open Giraffe.Razor
open Microsoft.AspNetCore.Mvc.ModelBinding

[<CLIMutable>]
type TestModel =
    {
        WelcomeText : string
    }

let model = { WelcomeText = "Hello, World" }

let viewData =
    dict [
        "Title", "Hello World" :> obj
        "Foo", 89 :> obj
        "Bar", true :> obj
    ]

let modelState = ModelStateDictionary()

let app =
    choose [
        // Assuming there is a view called "Index.cshtml"
        route  "/" >=> razorHtmlView "Index" (Some model) (Some viewData) (Some modelState)
    ]

validateAntiforgeryToken

The validateAntiforgeryToken http handler allows one to validate an anti forgery token created by the Microsoft.AspNetCore.Antiforgery API.

Example:

Inside a razor view add an anti forgery token:

<form action="/submit-sth">
   @Html.AntiforgeryToken
   <input type="submit">
</form>

Use the validateAntiforgeryToken function to validate the form request:

open Giraffe
open Giraffe.Razor

let invalidCSRFTokenHandler = RequestErrors.badRequest "The CSRF token was invalid"

let app =
    POST
    >=> route "/submit-sth"
    >=> validateAntiforgeryToken invalidCSRFTokenHandler
    >=> Successful.OK "All good"

Samples

Please find a fully functioning sample application under ./samples/GiraffeRazorSample/.

Nightly builds and NuGet feed

All official Giraffe packages are published to the official and public NuGet feed.

Unofficial builds (such as pre-release builds from the develop branch and pull requests) produce unofficial pre-release NuGet packages which can be pulled from the project's public NuGet feed on AppVeyor:

https://ci.appveyor.com/nuget/giraffe-razor

If you add this source to your NuGet CLI or project settings then you can pull unofficial NuGet packages for quick feature testing or urgent hot fixes.

More information

For more information about Giraffe, how to set up a development environment, contribution guidelines and more please visit the main documentation page.

License

Apache 2.0

About

Razor view engine http handlers for Giraffe web applications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PowerShell 72.6%
  • F# 27.1%
  • Shell 0.3%