Compiling scss files in ASP.NET MVC action

2019-08-28 23:53发布

are any alternatives for compile .scss files in action in ASP.NET MVC controller?

I have /Content folder with two files: main.scss and external.scss. In my controller I'm using NSASS package:

    public ActionResult GetCss(string scssPath)
    {           
        string scss = @"@import """ + Server.MapPath(scssPath) + @""";";
        var compiler = new SassCompiler();
        string compiled = compiler.Compile(source: scss);             

        return Content(compiled.ToString(), "text/css");
    }

my view:

<link href="@Url.Action("GetCss", "Theme", new { scssPath="~/Content/sass/main.scss" })" rel="stylesheet" type="text/css" />

main.scss file:

@import "external";

I have error

Error: File to import not found or unreadable: external

I tried to write @import "external.scss" but same problem.

1条回答
神经病院院长
2楼-- · 2019-08-29 00:19

We have the same (or similar problem). The problem I'm seeing is that SassCompiler is trying to find the @import files relative to the current working directory, instead of relative to the file which contains the @import.

There might be a few ways around this depending on what you're trying to do.

My workaround consisted of making a temporary copy of the directory structure and then updating all the @import statements in each file to make them relative to the working directory before compiling.


UPDATE I got this working without this hack by passing in all the paths to the 'includePaths' parameter. I had tried this before without success because I was using relative paths. If you use absolute paths then it works.

查看更多
登录 后发表回答