theLaw
theLaw

Reputation: 1291

MVC ASP.NET minify Views .cshtml

I have two simple questions:

  1. Is there a way to minify a .cshtml file like a JavaScript file or a CSS file?
  2. Are there any performance improvements if we minify all the views in a project?

Upvotes: 8

Views: 10863

Answers (3)

reZach
reZach

Reputation: 9429

There are many libraries to do such a thing. To answer your question, I have created a setup/process where your .cshtml files can be minified by a task runner within Visual Studio. The post itself describes this using .NET Core, but it is easily transferable over to .NET Framework if need be.

The benefits of this approach, versus other libraries, is I take additional steps to minify the .cshtml to further reduce size and improve performance.

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

The core library that does the minification is hosted in NPM: https://www.npmjs.com/package/gulp-cshtml-minify

Upvotes: 1

Tom Pažourek
Tom Pažourek

Reputation: 10167

For anybody interested in this, I built a simple HTML minification library that can be used with MVC 5:

https://github.com/tompazourek/RazorHtmlMinifier.Mvc5

It operates in compile-time instead of runtime, so it doesn't add any performance overhead. The minification is very simple (just replaces lots of spaces with one space).

Upvotes: 4

Alex from Jitbit
Alex from Jitbit

Reputation: 60566

This tool is a Razor compiler that minified HTML at precomile-time:

https://github.com/jitbit/HtmlOptimizerMvc4

Upvotes: 1

Related Questions