Minify and inline CSS for ASP.NET MVC

Updated on Wednesday, April 28, 2021

ASP.NET has a CssMinify class (and a JavaScript variant as well) designed for use in the bundling pipeline. But what if you want to have your CSS minified and inline? Here is an action that is working for me (rendered into a style tag on my _Layout.cshtml using @Html.Action("InlineCss", "Home")).

Note that I'm using this to inline CSS for this blog. The pages are cached so I'm not worried about how well this action performs. My blog is also basically all landing pages so I'm also not worried about caching a non-inline version for later use, I just drop all the CSS on every page.

(Related: Monitor page index status with Google Sheets, Apps Script and the Google Search Console API; Automate Google PageSpeed Insights and Core Web Vitals Logging with Apps Script; Using the Azure Monitor REST API from Google Apps Script)

(You might also like: Backup locked files on Windows 10: Volume Shadow Copy Update; Summer Solstice 2018; Route map and elevation profile for Hike Posts)

(All Code Posts)

Comments

Buyi Wen
i find a free online service to minify js(http://www.online-code.net/minify-js.html) and compress css(http://www.online-code.net/minify-css.html), so it will reduce the size of web page.
DavidGlulk

Thanks for the post!

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.

I Thought He Came With You is Robert Ellison's blog.

Newsletter

Related

GSC Monitor