I found a solution in Arran Maclean’s blog. He used Action Filter to remove all unneeded whitespaces after ASP.MVC 4 render the html files.
His Action Filter renders the perfect minified html file, all unneeded whitespaces are removed. However, I think it consumes a little memory and CPU resource to remove all whitespaces of a 20MB file.
Why we don’t remove the whitespace of razor view? I have researched several hours in Google but nobody thought like me. So I decide to write small winform application to remove all whitespace of my razor view before I deploy my project.
The original Razor view:
The Razor view hash been removed whitespaces:
Here are my test results.
The “No trimming” section indicating that the server returns a 11.1MB file in about 1.85-3.45 seconds. The “Runtime trimming” section indicating that the server removes all whitespaces by Action Filter and returns a 6.4MB file in about 3.36-4.07 seconds. The last section indicating that the server renders HTML from a minified razor view and returns a 6.5MB file in 1.35-2.71 seconds.
My application doesn’t have the best algorithm to minify the razor view so I can’t afford the best result. However my solution reduces a little consumed resource of server
You can get my source code at here: https://github.com/haison8x/razor-white-space-trimming. You are welcome for improving my algorithm and quoting my blog.
Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!
Trả lờiXóaMelbourne Mobile Developer
jacob-phan
Trả lờiXóaThanks. However I think this strategy isn't useful anymore because the network is very good now. We don't really need to trim the text
Trả lờiXóa