WebMarkupMin + Razor + React:无法将标记缩小应用于以 'gzip' 格式编码的文本内容

WebMarkupMin + Razor + React: Can not apply markup minification to text content, that was encoded in the 'gzip' format

我在 'ASP.NET Core with React.js' 项目中混合使用 React.js SPA 生成页面和 Razor 视图。

实施WebMarkupMin时,运行时出现以下异常:

An unhandled exception occurred while processing the request.

InvalidOperationException: Can not apply markup minification to text content, that was encoded in the 'gzip' format.

为什么会发生这种情况,我该如何预防?

出现异常是因为 WebMarkupMin 正在尝试缩小已经压缩的数据。

页面已被 Node.Js ('npm start') 压缩。

要解决此问题,您可以像这样从 WebMarkupMin 中排除 React.js SPA 网址:

var reactUrls = new List<IUrlMatcher>(){ new ExactUrlMatcher("/")};
services.AddWebMarkupMin()
            .AddHtmlMinification(options => options.ExcludedPages = reactUrls)
            .AddHttpCompression(options => options.ExcludedPages = reactUrls);

为了将您的 React.js 路由指向 ASP.NET 托管 Razor 视图(由 WebMarkupMin 处理),您可以执行以下操作:

render() {
    const reload = () => window.location.reload();
    return (
        <Switch>
            <Route exact path='/razor' onEnter={reload} />
            <Route exact path='/mvc' onEnter={reload} />
            <Route exact path='/' component={HomePage} />                   
            <Route component={NotFoundPage} />
            <Route path='' onEnter={reload} />
        </Switch>           
    );