Reputation: 431
Is there any event (or a filter ) that is triggered ( called) after rendering all output html MVC5 in C #.
I would use this event to remove whitespace between tags decreasing the size of the html
Please some help??? :)
Upvotes: 2
Views: 1420
Reputation: 113242
You can still use Response.Filter
in MVC, so you can define an ASP.NET filter (a class derived from Stream
that wraps the output stream and when written to by ASP.NET writes something to that stream).
A convenient way to do this is by combining with the MVC form of filters, so you subclass ActionFilterAttribute
to create an attribute that then in the OnActionExecuting
sets filterContext.HttpContext.Response.Filter
to the minifying filter.
Alternatively, if you were going to use it in the majority of cases, you could apply the filter in a module or global.asax.
All that said though, minifying tends not to save a lot in the case of HTML; there isn't as much that you can change without breaking anything compared to some other formats, and much of the reduction you get is nullified by the fact of applying Gzip compression, which you should have already applied if you cared about the effect of size on download time, because Gzip tends to work particularly well with the sort of blocks of whitespace that minifying would remove, so you might e.g. have a 100KiB page that Gzips to 25KiB that you turn into a 90KiB page that GZips to 25KiB.
Upvotes: 0
Reputation: 54618
You haven't explained any reason to do so (first it sounds like a sad case of micro-optimization, not a good place to spend time, and secondly you haven't explained what decreasing the size of html will solve aka The XY Problem).
By default IIS/IIS Express uses GZIP to compress your HTML:
Response Headers:
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
If you're looking for minification, the results of well known websites being minified have shown only to improve them by 9-16% and remember this are extremely complicated and big websites. Unless you have an extremely large website, it's most likely not the best place to spend time in optimization. If you're bound determined, then you simple need to look for a .Net/IIS html minification plugin (don't rewrite whats already written if you can help it).
Upvotes: 3