Giraffe: How to Serve Static Files using XmlViewEngine

Created on 11 Oct 2017  路  10Comments  路  Source: giraffe-fsharp/Giraffe

Using the razor view engine, I can include css files just by having something like <link rel="stylesheet" href="~/css/site.css" /> in my _Layout.cshtml file

Trying out the XmlViewEngine, I have the following

let index =
    html [] [
        head [] [
            title [] [ rawText "Giraffe Test" ]
            link [ attr "href" "/Site.css"; attr " rel" "stylesheet" ]
        ]
        body [] [
            div [ attr "id" "header" ] [
                h1 [] [
                    a [ attr "href" Path.home ] [
                        rawText "Giraffe Test"
                    ]
                ]
            ]
            div [ attr "id" "main" ] []
        ]
    ]

which give me an 404 (Not Found) error trying to get http://localhost:5000/Site.css. I check that the file is in the output directory.

The way that Suave did it. There is a pathRegex that check for the file type and a Files.browseHome function that serve the static files. I did not see the equivalent functions in Giraffe.

question

All 10 comments

Are you using static middleware?

Do you mean app.UseStaticFiles()? I have it if so.

the paths not matching one is root one is /css/ (although that might just be for example)

if not using root, may have to config middleware to allow specific folder

example of extending allowed folders (it's bit ugly)

let configureApp (app : IApplicationBuilder) = 
    app.UseStaticFiles( 
             StaticFileOptions(
                 FileProvider = 
                     new PhysicalFileProvider(
                             Path.Combine(Directory.GetCurrentDirectory(), @"dist")),
                 RequestPath = PathString("/dist")
         )
        ) |> ignore

Turn out my problem was that I put the css file inside my project root instead of inside the WebRoot folder. oops

@gerardtoconnor Thanks for the example, I'll keep that in mind for next time.

I think we should create a file handler to automate this without the need of static file middleware, one that does not use regex though, just folder & list of ext

The middleware I'm using is part of ASP.NET Core so I don't know if it is important to not rely on it given Giraffe's purpose. I do think that it is an interesting discussion since it adds parity with Suave and allows more customization.

Is the dependency on Microsoft.AspNetCore.StaticFiles really such a burden that we really need a custom Giraffe handler for files? It just feels like solving a non-existing problem here.
Could someone elaborate what the advantage would be?

I'm also not quite sure what a Giraffe static file handler would add which the ASP.NET Core middleware doesn't already do. I know it is different than Suave, but Giraffe doesn't aim to be as close to Suave as possible, but rather glue in nicely with ASP.NET Core.

My view is that in a modern/cloud optimised web application there's probably generally very few static files which would get served from the web application server anyways (maybe things like favicons, etc.), and the majority of static files probably get served from a CDN or other cloud storage solutions which have the files much closer to the edge than the web app server is, but maybe I'm wrong.

The reason I was batting the idea was due to obsession of throughput efficiency, but for the most part, for most uses using built in is fine.

The overhead that using StaticFiles introduces is that it is checking for file every request regardless of being before or after giraffe, luckily it can filter out by directory permissions before actually hitting the underlying filesystem but under tree router it is would be more efficient due to not duplicating checks ... the benefit is likely minimal so I'm generally in agreement, but I also like the idea of staying away from app config in favour of handlers, given configuring StaticFiles for custom locations/files is ugly.

Just to explain my original thinking/suggestion, stick to StaticFiles though

Was this page helpful?
0 / 5 - 0 ratings