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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.