1

In ASP.NET MVC 5 you would achieve this by:

public ActionResult DynamicJs()
{
 // dynamically generated
 string javaScript = new Minifier().MinifyJavaScript("alert('Hello world!');");
 // returns minified javaScript
 return JavaScript(javaScript);
}

The Minifier class was a member of Microsoft.Ajax.Utilities, which you would get from the WebGrease Nuget package.

However, in ASP.NET Core this package is not available for .NET Core and many are using the BundlerMinifier.Core package by Mads Kristensen for minification. https://www.nuget.org/packages/BundlerMinifier.Core/3.2.449

How can I achieve the same result in ASP.NET Core?

Jeremy Caney
7,771111 gold badges56 silver badges86 bronze badges
asked Jul 18, 2020 at 7:10

1 Answer 1

4

NUglify is the underlying dependency for BundlerMinifier.Core that does all the heavy lifting.

You can use it to achieve the same result.

//dynamically generated
string javaScript = "alert('Hello world!');";
//set ContentType as the JavaScript() object is not available in .NET Core
ContentResult result = new ContentResult
{
 ContentType = "application/javascript", 
 Content = NUglify.Uglify.Js(javaScript).Code
};
 
return result;

Uglify also has methods for CSS and HTML.

enter image description here

answered Jul 18, 2020 at 7:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.