↘️ Trivial compile-time Razor HTML Minifier for ASP.NET MVC 5.
Trivial compile-time Razor HTML Minifier for ASP.NET MVC 5.
Binaries of the last build can be downloaded on the AppVeyor CI page of the project.
The library is also published on NuGet.org, install using:
PM> Install-Package RazorHtmlMinifier.Mvc5
RazorHtmlMinifier.Mvc5 is built for .NET v4.8 with a dependency on ASP.NET MVC 5.3.0 and System.Web
.
Find the Web.config with your Razor configuration (by default it's in Views/Web.config
). You should see something like this inside:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.3.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
In order to start minifying views and partial views, replace it with (after the NuGet package is installed):
<host factoryType="RazorHtmlMinifier.Mvc5.MinifyingMvcWebRazorHostFactory, RazorHtmlMinifier.Mvc5, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a517a17e203fcde4" />
Then rebuild your solution, which should also restart the app.
If you're using Razor @helper
functions placed inside the App_Code
folder, you have to do additional configuration in order to minify those. In the root Web.config
, you should see something like this:
<compilation debug="true" targetFramework="4.8" />
Your <compilation>
element might have different attributes. Leave the current attributes as-is, and add <buildProviders>
like so:
<compilation debug="true" targetFramework="4.8">
<buildProviders>
<add extension=".cshtml" type="RazorHtmlMinifier.Mvc5.MinifyingRazorBuildProvider, RazorHtmlMinifier.Mvc5" />
</buildProviders>
</compilation>
The minifier processes the code generated during Razor compilation. Because it runs in compile-time, it shouldn't add any overhead during runtime.
The entire source code is just two files, feel free to view them.
The minification algorithm is fairly trivial. It basically:
\n
);The minification process is deliberately trivial so that it would be easy to understand and expect.
CAUTION! The minification is not context-sensitive, and it doesn't have any special handling of <pre>
tags or similar. If you use <pre>
tags or have any other significant white-space in your HTML, you shouldn't use this library.
The code is inspired by Meleze.Web (an older project), but it's much simplified and updated to be used for with the latest version of ASP.NET MVC.
When you change the Razor factory, you might experience IntelliSense issues in Visual Studio.
I've investigated this and it looks like VS actually needs to have the assembly in GAC for IntelliSense to work. Luckily, now when the latest version of the library is strong-named, it's possible to add it to GAC. Adding the assembly to GAC shouldn't have any side-effects, but it will need to be added on every developer machine that wants to use see the IntelliSense in Razor files completely (without the squiggly undelines), which can be a hassle.
If you want to add the assembly to GAC, you'll need to do the following:
Developer Command Prompt for VS
(you'll find it in Start menu) as an Administrator.cd "C:\PATH_TO_YOUR_SOLUTION\packages\RazorHtmlMinifier.Mvc5.2.0.0\lib\net45"
gacutil /i RazorHtmlMinifier.Mvc5.dll
(it should respond Assembly successfully added to the cache
)Then it should start working.
There are also other alternative solutions:
Global.asax
. You'd have to use the WebConfigurationManager
API to modify the Web.config during runtime.However, I wouldn't recommend either of those (I also haven't tried them), as they feel more like a hack, and might cause more issues if you'd be doing stuff like precompilation of the views. Adding the assembly to GAC is probably the easiest, it's still annoying that VS requires that though...