this is online tool of ASPOSE to Minify HTML, but there is no documentation that how can we use this functionality in c#
We checked and found that this application used some minification methods native to ASP.NET. That is why the code is not showcased in our documentation because it was not related to Aspose.HTML. ASP.NET provides methods to minify the assets. You can check it on internet and implement at your end. However, the code used behind that app is below:
using System;
using System.IO;
using BundlerMinifier;
namespace Aspose.Html.Apps.Handlers.Applications.HtmlMinifier
{
public class BMMinifier : IMinifier, IDisposable
{
private BundleFileProcessor _processor;
private string _guid;
private string _workDirectory;
public BMMinifier()
{
_processor = new BundleFileProcessor();
_guid = Guid.NewGuid().ToString();
}
public string Minify(string content, MinifyContentType type)
{
var currDir = Directory.GetCurrentDirectory();
string path = Path.Combine(Path.GetTempPath(), "BundlerMinifier", _guid);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
_workDirectory = path;
Directory.SetCurrentDirectory(_workDirectory);
var extension = type.ToString().ToLowerInvariant();
var filename = $"temp.{extension}";
try
{
var bundle = new Bundle();
bundle.IncludeInProject = true;
bundle.OutputFileName = $"temp.min.{extension}";
bundle.InputFiles.AddRange(new[] { filename });
string csspath = Path.Combine(path, filename);
File.WriteAllText(csspath, content);
string bundlePath = Path.Combine(_workDirectory, $"bundle.json");
BundleHandler.AddBundle(bundlePath, bundle);
_processor.Process(bundlePath);
var minified = File.ReadAllText(bundle.OutputFileName);
return minified;
}
catch (Exception ex)
{
if (ex is FileNotFoundException)
{
throw new FormatException("Invalid input code format.");
}
else throw;
}
finally
{
Directory.SetCurrentDirectory(currDir);
}
}
public void Dispose()
{
if (Directory.Exists(_workDirectory))
{
try
{
Directory.Delete(_workDirectory, true);
}
finally { }
}
}
}
}