Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [167]

By Root 5261 0
results in waste of time rather than an improvement.

Beyond this, consider that script files are rich with white spaces and blanks. Simply removing these characters can cut a significant percentage of software fat out of the file. This is just what minifiers are for.

A minifier is a tool that parses a given script file and rewrites it in a way that is functionally equivalent to the original but devoid of any unnecessary characters. The jQuery library is commonly served in its minified form. A minified script file is nearly impossible to read or understand for a human, so I don’t recommend using minified files during development.

Microsoft released a minifier tool; you can get it at http://aspnet.codeplex.com/releases/view/40584. This tool can work on script and CSS files. Also, in addition to removing white spaces and blanks, it safely attempts to reduce curly brackets and to make variable names shorter.

Note

You might want to look at build-time minifier tools such as Chirpy because having to manually minify several files can be a bit of a pain. See http://chirpy.codeplex.com.

Localized Scripts


Like other Web resources, scripts can be subject to localization. At the very end of the day, a script is a relatively long string of text, so there’s really nothing that prevents you from embedding a script into the application resources along with a plain RESX file.

The method GetWebResourceUrl on the ClientScript property of the Page class can be used to return the URL to any resource stored in a satellite (localized) assembly. In this way, you link your scripts from the assembly, deploy the localized assembly, and you’re done.

The only other alternative you have is maintaining different copies of the script and resolve the name programmatically. In ASP.NET 4, the ScriptManager control can streamline this task quite a bit. Here’s how to use the script manager component:

When the property EnableScriptLocalization is true, the section lists all script files to be downloaded that might be subject to localization. Localization consists of mangling the provided name of the script in a way that incorporates culture information. For example, the preceding code will emit the following markup if the UI culture is set to Italian: