Servicestack contents of an embedded resource

So I know I can use servicestack gap to embed resources into a dll…those resources must be loaded internally…so is there a method that is exposed or can be exposed as api so that I can read the contents of an embedded resource.

I’m trying to use servicestack razor as a fast email template engine, if I have the above I know I will be able to use a helper* to embed an inline css tag and I might not even need Premailer.NET to run on the after.

*much like this: only replacing the file lookup with embedded call.

This would be really good, otherwise I’d have to use slower razorengine library or always inline styles myself.

You can access embedded resources via the Virtual File System where you can read the contents of a file in a Service with:

var file = VirtualFileSources.GetFile("path/to/file.cshtml");
var razorHtml = file.ReadAllText();

Outside a Service you can use the HostContext.VirtualFileSources singleton.

If it helps here’s a generic Service that resolves and returns the contents of any File:

public class GetFile
    public string Path { get; set; }

public class FileServices : Service
    public object Any(GetFile request)
        var file = VirtualFileSources.GetFile(request.Path);
        if (file == null)
            throw HttpError.NotFound("File '{0}' does not exist".Fmt(request.Path));

        return new HttpResult(file) {
            ContentType = MimeTypes.GetMimeType(file.Extension)

Which you can call with:


Awesome, it looks like this will work well (and I won’t even need that service), for other’s ref:

  1. Need to change your css files build action to ‘Embedded Resource’

  2. Here is simple Embedcss function that can use.

    using System;
    using ServiceStack;
    using ServiceStack.Html;

    public static class HtmlHelpers
    public static MvcHtmlString EmbedCss(this HtmlHelper htmlHelper, string relativePath)
    var file = HostContext.VirtualFileSources.GetFile(relativePath);
    var cssText = file.ReadAllText();
    return htmlHelper.Raw($"\n{cssText}\n");
    catch (Exception ex)
    throw new Exception("EmbedCss - see Inner: ", ex);

like: @Html.EmbedCss(“css/htmlreport.css”)

  1. If you want to support gmail client that still need …but currently needs new release…
1 Like