Minify Html output of ASP.NET Application

后端 未结 6 2070
后悔当初
后悔当初 2020-11-28 23:47

What are the ways by which we can reduce the size of the HTML Response sent by an asp.net application?

I am using Controls which are not owned by me and it produces

6条回答
  •  萌比男神i
    2020-11-29 00:17

    Just adding another option I do not see listed here, which is the one I was recommended using:

    Html minifier command line tool

    Usage: here and here

    There is an issue, however, with this tool: it leaves single line (//) comments, and it causes problems for Razor parsing, since a single line comment placed within a C# block like the following:

    @{
      ... 
      ...
      // anything
      ...
    }
    

    will cause the minification output rest of the line, from this point on, to be ignored by the Razor parser, which will thus raise an error stating there it could not find the closing "}" for the block.

    My workaround for this issue was to completely removing these comments from the output. This way it works. To do that, simply remove the RegexOptions.SingleLine from line 145:

    htmlContents = Regex.Replace(htmlContents, @"//(.*?)\r?\n", ""/*, RegexOptions.Singleline*/);
    

提交回复
热议问题