8

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?
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
theLaw
  • 1,261
  • 2
  • 11
  • 23
  • 1
    What the point? The goal of minifying js files is to make them weight less (-> less kb traffic). a cshtml is complied on server side, so i think there is no point in minify it – DmitryK Mar 04 '14 at 14:33
  • Minify the html and the inside it would not speed up the view? – theLaw Mar 04 '14 at 14:35
  • 5
    Maybe he means how to minify the html output not the actual `.cshtml` file. In that case this may help http://stackoverflow.com/questions/2104513/minify-html-output-from-an-asp-net-mvc-application – JOBG Mar 04 '14 at 14:35
  • i'm asking this cause i need to have the more lightwave interface that i can have, i have very slow windows ce embed and slow network. – theLaw Mar 04 '14 at 14:47
  • 1
    well, if you are running dedicated IIS6+, you can turn on the IIS compression: http://msmvps.com/blogs/omar/archive/2006/08/10/iis-6-compression-quickest-and-effective-way-to-do-it-for-asp-net-compression.aspx which can be helpful – DmitryK Mar 04 '14 at 14:59

3 Answers3

4

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).

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
1

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

https://github.com/jitbit/HtmlOptimizerMvc4

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
1

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

reZach
  • 8,945
  • 12
  • 51
  • 97