Using MVC optimization framework with ServiceStack.Razor

I have a small project running on ServiceStack (if you don't know
what ServiceStack is, I recommend you go and read up on
!) which uses ServiceStack from top
to bottom (ServiceStack.Razor right the way down to

I ran into an issue where I wanted to use bundling and minification
support from Microsoft.AspNet.Web.Optimizations. I realise that
there is a
ServiceStack.Bundler project, but I am familiar with the MVC bundling and minification
framework and wanted to use that. I ran into a few problems with this so
thought I would share with people how they can achieve the same thing.

  1. Download Microsoft.AspNet.Web.Optimizations from NuGet into
    your project. This has two dependencies which will be brought along for the ride:

    1. Microsoft.Web.Infrastructure
    2. WebGrease
  2. If you do not already have a BundleConfig class in your App_Start
    folder then create one. The class should look something like this:

    public class BundleConfig
        public static void RegisterBundles(BundleCollection bundles)
            bundles.Add(new StyleBundle("~/bundles/css/core").Include(
  3. Make sure that the RegisterBundles method we just created is
    called from you Global.asax

    public class MvcApplication : System.Web.HttpApplication
        protected void Application_Start()
  4. You will need to ensure that your pages know about
    System.Web.Optimization, before you can reference the Styles and Scripts static classes to access your bundles.

        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="ServiceStack.Razor.ViewPage">
                <add namespace="ServiceStack.Html" />
                <add namespace="ServiceStack.Razor" />
                <add namespace="ServiceStack.Text" />
                <add namespace="ServiceStack.OrmLite" />
                <add namespace="System.Web.Optimization"/>
  5. With all this in place, you should be able to start referencing the
    bundles from your ServiceStack.Razor pages, like this:

    <!doctype html>
            <title>iSAMS Error Reporting</title>
  6. This however did not work and ended up rendering the style tag out
    html encoded. This meant that the styles were not applied to the markup and the link tag was actually rendered to screen. I resolved this issue, by using the ServiceStack.Text static method AsRaw() to make sure the output was not HTML encoded.

    <!doctype html>
            <title>iSAMS Error Reporting</title>

This now works as expected.

If someone else knows a better way of doing this, please do let me know.