Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how return css file via endpoint? #617

Open
darren-mk opened this issue Sep 14, 2024 · 2 comments
Open

how return css file via endpoint? #617

darren-mk opened this issue Sep 14, 2024 · 2 comments
Labels
question General question

Comments

@darren-mk
Copy link

Hi,

I see the following functions that emits text/html in Giraffe namespace but can't find one for text/css. How can I send css file through an endpoint?

Thanks,

    /// <summary>
    /// Writes a HTML string to the body of the HTTP response.
    /// It also sets the HTTP header Content-Type to text/html and sets the Content-Length header accordingly.
    /// </summary>
    /// <param name="html">The HTML string to be send back to the client.</param>
    /// <returns>A Giraffe <see cref="HttpHandler" /> function which can be composed into a bigger web application.</returns>
    let htmlString (html : string) : HttpHandler =
        let bytes = Encoding.UTF8.GetBytes html
        fun (_ : HttpFunc) (ctx : HttpContext) ->
            ctx.SetContentType "text/html; charset=utf-8"
            ctx.WriteBytesAsync bytes

    /// <summary>
    /// <para>Compiles a `Giraffe.GiraffeViewEngine.XmlNode` object to a HTML view and writes the output to the body of the HTTP response.</para>
    /// <para>It also sets the HTTP header `Content-Type` to `text/html` and sets the `Content-Length` header accordingly.</para>
    /// </summary>
    /// <param name="htmlView">An `XmlNode` object to be send back to the client and which represents a valid HTML view.</param>
    /// <returns>A Giraffe `HttpHandler` function which can be composed into a bigger web application.</returns>
    let htmlView (htmlView : XmlNode) : HttpHandler =
        let bytes = RenderView.AsBytes.htmlDocument htmlView
        fun (_ : HttpFunc) (ctx : HttpContext) ->
            ctx.SetContentType "text/html; charset=utf-8"
            ctx.WriteBytesAsync bytes
@nojaf
Copy link
Contributor

nojaf commented Sep 16, 2024

Hello,

You might want to consider using the static files middleware, as outlined in the documentation at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-8.0.

It seems Giraffe doesn't offer any other built-in solutions for this.

Have you explored potential solutions at https://giraffe.wiki/docs? The current content is lacking, and I believe we should add a new section there, even if it doesn't specifically pertain to Giraffe.

@dustinmoris
Copy link
Member

That's a good idea, it could be added similarly how Response Caching was mentioned. It's mostly deferring that responsibility to the already available ASP.NET Core Middleware and I'd highly encourage to continue doing that wherever possible. The original ethos of Giraffe was always to make ASP.NET Core more functional and great experience for F# as well, not so much to complete with it doing things differently. Unless there is really good reason why Giraffe should have its own static file handler I think the ASP.NET Core middleware is the preferable option.

Nothing prevents one though to expose a file themselves through a Giraffe handler. In HTTP there is no difference between a static file or a dynamic JSON response, both is just data returned in the HTTP body with slightly different headers. Someone can always stream the bytes from a file into the response and set the required HTTP headers (e.g. application/pdf or image/png, etc.) for the type of content that is being returned. It's just a plain handler like everything else.

@64J0 64J0 added the question General question label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

4 participants