
Automatically minifying .cshtml files in .NET Core - bobblywobbles
https://debugandrelease.blogspot.com/2018/11/automatically-minifying-cshtml-files-in.html
======
romanovcode
I think there is a better way.

1\. Add package called `WebMarkupMin.AspNetCore2`.

2\. Add and configure code in your `Startup.cs` like so.

    
    
      services.AddWebMarkupMin(
        options =>
        {
          options.AllowMinificationInDevelopmentEnvironment = true;
          options.AllowCompressionInDevelopmentEnvironment = true;
          options.DisablePoweredByHttpHeaders = true;
        })
        .AddHtmlMinification(
          options =>
          {
            options.MinificationSettings.RemoveRedundantAttributes = true;
            options.MinificationSettings.RemoveHttpProtocolFromAttributes = true;
            options.MinificationSettings.RemoveHttpsProtocolFromAttributes = true;
            // More options if you like so
          })
      .AddHttpCompression();
    

Not only will this minfy HTML output, but also apply a lot more optimisations
based on your configuration.

~~~
bobblywobbles
I'm open to adding more of the options that WebMarkupMin provide.

The main difference between this and WebMarkupMin is that WebMarkupMin runs at
runtime - therefore it is costing time to minify when you can already minify
the files before launching your web application.

~~~
romanovcode
Yeah, that is true. However that HTML is usually cached so not a big deal.

Also, this reduces complexity of the project. Both methods are fine, it's just
I prefer this one.

------
GordonS
In all seriousness, is it really worth the effort and added complexity, when
HTML is going to be compressed on the wire? What are you going to save, maybe
a few bytes?

~~~
eksemplar
I'm not sure anyone who truly cares about effort uses .net core. :p

I mean, it's a cheap shot, but it's frankly so unproductive compared to Django
it's basically a running joke. Not that .net core doesn't see a lot of usage,
much like angular it's very popular in the european public sector, but it's
just such a waste of resources. By resources I mean, man hours by the way,
because it's really effective technically, it's just that, all that you can
pay for around a million years of AWS/Azure time for the extra wage-hours you
need to pay programmers when you use .net instead of something useful.

And in two years it'll be obsolete anyway, because by then Microsoft will have
completely changed their direction, again. I say this, as as someone who's
running Azure and 365 as our cloud provider for the majority of our services,
as someone who's genuinely happy with the services Microsoft provides and
having had a great 25 year long business relationship with them, but as far as
development goes, .net is just too crazy to be worth it these years.

~~~
GordonS
I actually love working with .NET Core, and I'm really productive with C#. For
some background, I'm pretty much a polyglot; while C# is where I spend most of
my time, I also regularly work with JavaScript and Typescript, and
occasionally with Java, PHP and C.

I also find it preposterous that .NET Core will be obsolete in 2 years, and
I'm not sure what you mean about Microsoft changing direction "again" \- the
.NET Framework was released almost _2 decades_ ago, and I find the recent move
to a cross-platform runtime to make for exciting times!

~~~
AchieveLife
Have you tried the SAFE stack with .Net core?

[https://safe-stack.github.io/](https://safe-stack.github.io/)

~~~
GordonS
I haven't, but it looks really interesting!

I've only dabbled with F#, but I always enjoyed it when I do - I just wish I
could find the time to spend on it :(

------
Novashi
Neat, but I thought the view compilations were cached and optimized already?
Wouldn't it be better to add a pipeline that minifies the compiled views
instead of having files on disk?

~~~
bobblywobbles
I believe the following project does exactly that: WebMarkupMin.AspNetCore2.
The one consideration to this project is that the minifications are done at
runtime.

Doing the minification before the application starts saves time from a
pipeline minifying on the fly.

~~~
Novashi
>Doing the minification before the application starts saves time from a
pipeline minifying on the fly.

That would only affect a cold-start (app pool recycle) or a view recompile
before that'd be an performance concern, unless I don't understand how/when
views are cached and then evicted from cache.

If you have a lot of views with a lot of partial views inside them, then yeah,
you'd only want to run minification once on the final view, instead of for
every partial view. I'm not sure if you'd have (or want) that much control
over the view rendering process.

Btw it's a cool post if you're the author.

~~~
bobblywobbles
Yes, those points are true.

And yes, I am the author.

------
caseymarquis
Neat, but I’m curious to know how many people are using server side templating
with dotnet core? I avoid it like the plague these days, using vue/webpack and
an http api. If there’s actually a lot of razor still out there, I should
probably keep up with the latest version of it.

~~~
thrower123
A lot of times, that's all you need. For quick and dirty internal tools, its a
godsend. I'd be happy to use it more, and cut out the whole Gordian Knot of
overburdened front-end development.

~~~
caseymarquis
I’m hoping that wasm leads to a unified dotnet stack with fantastic tooling
and shared classes for front-end development. Best of both worlds at that
point. That said, I resisted getting into the JavaScript front-end ecosystem,
but I really like it at this point.

------
NicoJuicy
I knew how to do it, but the combined files are new through the settings file:
MinifiedCshtml. I knew how to do that also ( manually) in the csproj file (
similar to resource files), but not how to do it automaticly (y)

Thanks !

~~~
bobblywobbles
You are welcome, I glad it helped.

------
rocky1138
Does this actually do anything? Why not just gzip the transmission?

~~~
bobblywobbles
Minification along with gzip gives slightly better compression than with gzip
alone. Even if it is a slight gain, why not set it up once and forget it?

~~~
userbinator
You can think of it like PNG vs JPG --- the former is entropy-coding only, the
latter has a lossy transform before entropy coding. Minification is a lossy
transform, and if it didn't provide any benefits, no one would be using JPG or
any lossy video format.

