Online Book Reader

Home Category

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

By Root 5710 0

You can even embed the reference to the image into the CSS as shown here:

The CSS class is defined as follows:

.UserInformation {

width:123px;

height:115px;

background-image:url(sprite.png);

background-position:-0px 0;

}

In other words, you pinpoint the fragment of the sprite you like using CSS attributes such as background-position, background-image and, of course, width and height.

Microsoft is currently working on an extension to ASP.NET 4 that supports sprites. For more information, check out http://aspnet.codeplex.com/releases/view/50140.

Note

Image inlining is another potentially useful technique for dealing with images and static resources more comfortably. Image inlining consists of streamlining a Base64-encoded version of the image file into a CSS file or an HTML page. As of today, very few browsers support this technique and, in addition, the Base64 encoding increases the size of individual images, making for a large download.

External References vs. Inline Content


This is one of those evergreen questions that are revamped periodically in geek talks. Is it better to embed script and style sheets (and to some extent images) into a page, or is it preferable to keep several distinct references that the browser can deal with?

External references increase the number of HTTP requests being made, but they keep the page size smaller (often significantly smaller) and, more importantly, can be cached by the browser. Frankly, inline content is a great thing at development time where, instead, the effects of browser caching can be quite annoying. For deployed sites, browser caching saves you HTTP requests and is a feature that you can fine-tune when preparing the response for a given page or resource.

As mentioned, just reducing the number of HTTP requests might not ensure optimal performance. You should work in two directions and try to produce a magical mix of fewer HTTP requests for not-so-large resources.

Note

To measure the performance and quality of Web pages, you can use YSlow—a Firefox add-on integrated with the Firebug Web development tool. (See http://developer.yahoo.com/yslow.) Based on a set of commonly accepted best practices, the tool analyzes a Web page and provides recommendations for improving the overall performance. As far as Internet Explorer is concerned, Internet Explorer 9 comes with the IE9 Developer toolbar, which provides similar capabilities.

Summary


In this chapter, we examined a few issues you might face when building pages and interacting with them—errors, personalization, and resource handling.

Often, good programs do bad things and raise errors. In the Web world, handling errors is a task architecturally left to the run-time environment that is running the application. The ASP.NET runtime is capable of providing two types of error pages, both of which are not very practical for serious and professional applications, although for different reasons. When a user who is locally connected to the application does something that originates an error, by default ASP.NET returns a “geek” page with the stack trace and the full transcript of the exception that occurred. The remote user, on the other hand, receives a less compromising page, but certainly not a user-friendly one. Fortunately, though, the ASP.NET framework is flexible enough to let you change the error pages, even to the point of distinguishing between HTTP errors.

Personalization allows you to write pages that persist user preferences and parametric data from a permanent medium in a totally automated way. As a programmer, you’re in charge of setting up the personalization infrastructure, but you need not know anything about the internal details of storage. All you do is call a provider component using the methods of a well-known interface.

Finally, modern Web pages are much more than just HTML markup. Script files, images, CSSs, and literals need to

®Online Book Reader