fhevol
fhevol

Reputation: 996

ASP.NET Core MVC 2.1 View Compilation on Publish to Azure

I created a class that inherits from RazorProjectFileSystem that performs minification on cshtml files prior to them being compiled. This is configured in Startup.ConfigureServices.

Works just fine when running locally, stripping out a mass of whitespace and comments. However when the site is published to Azure a ProjectName.Views.dll is created, which completely bypasses my logic.

I still want to use precompiled views (and yes, we are using gzip compression as well), but will need to plug my logic somewhere else in the chain to get this to happen. However I can't find any information on where the .cshtml files are read from the file system as part of the creation of the View.dll.

I suspect we may need to run the minification prior to the actual build process itself. Any ideas or recommendations very much appreciated.

Upvotes: 0

Views: 80

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 5294

There are many libraries available to do build time minification of CSHTML files, you can use **gulp-cshtml-minify** . Only problem with this one is that you have to install node and gulp for using this.

Here is a detailed elaboration on this. which you can find it in below blog:

https://debugandrelease.blogspot.com/2018/11/automatically-minifying-cshtml-files-in.html

Only thing which you have to do is append .min word while pulling the vies from the folder like below:

public override ViewResult View(string viewName, object model)
        {
            if (!IsDevelopment())
            {
                string action = ControllerContext.RouteData.Values["action"].ToString();

                if (string.IsNullOrEmpty(viewName))
                {
                    viewName = $"{action}.min";
                }                    
                else if (viewName == action)
                {
                    viewName += ".min";
                }
            }

            return base.View(viewName, model);
        }

Hope it helps.

Upvotes: 1

Related Questions