Can't get System.Web.Optimization to run with

2019-04-14 22:45发布

问题:

Has anyone had any luck using System.Web.Optimization with Nancy Self Hosting? If I comment out "Styles.Render("~/csspack/logincss").ToString()" the view works fine. If I leave it in a blank page is sent to the browser.

This is what my Razor config looks like.

public class RazorConfig : IRazorConfiguration
{
    public IEnumerable<string> GetAssemblyNames()
    {
        yield return "Microsoft.Web.Infrastructure";
        yield return "WebGrease";
        yield return "System.Web.Optimization";
        yield return "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
    }

    public IEnumerable<string> GetDefaultNamespaces()
    {
        yield return "Microsoft.Web.Infrastructure";
        yield return "WebGrease";
        yield return "System";
        yield return "System.Web";
        yield return "System.Web.Optimization";
        yield return "Nancy.ViewEngines.Razor";
    }

    public bool AutoIncludeModelNamespace
    {
        get { return false; }
    }
}

I registered it in startup like this.

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
            container.Register<IRazorConfiguration, RazorConfig>().AsSingleton();

            container.Register<RazorViewEngine>();

          .....bundle code...
}

This is my view.

inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@using Nancy.Helpers
@using System.Web.Optimization

<!DOCTYPE html>
<html lang="en">
<head>
<head>
    <meta charset="utf-8" />
    <title>Log In</title>
    @Html.Raw(Styles.Render("~/csspack/logincss").ToString())
</head>
.... more html ....

回答1:

I couldn't get this to work, so I ended up switching to Cassette. http://getcassette.net/

Worked right out of the gate with no issues. Didn't really have to change much. Looks like System.Web.Optimization borrowed a lot from Cassette.

Update
Cassette was giving me weird performance issues and errors when running on Linux. I ended up just building my own. https://github.com/donnyv/Nancy.BundleIt



标签: c# razor nancy