IS Department

ASP.NET Tips and Tricks

Posted in Programming by Brian Russell on November 12, 2006

ASP.NET Tips and Tricks by Scott Guthrie

He posted the demos and slides for this talk at his website

Choosing between the two different models of development for ASP.NET sites, he offered these tips:  If your site is content centric, you will probably want to use the web site model.  If your background is project based and you are familiar with it, you should use the web application project model.

Scott then showed that you can make any project or item a template very easy.  This is good for doing the repetitive stuff that is done every time a new project is started, or when you are adding a new item to a project.  Stop wasting time and build templates.

He talked about the CSS control adapters toolkit.  This toolkit allows developers to override rendering of controls.  It lets the adapter render the controls.  Right now the Menu, TreeView, data controls, and login controls have control adapters built for them.  This tool kit is going to be incorporated into the product next release, so start using them if you want.

By placing the debugger keyword in your javascript code, you can debug in Visual Studio.

If you are debugging ASP.NET AJAX code, they allow you to step into their javascript libraries.  The normal javascript code output to the browser is compact and void of comments.  If you are running your site in debug mode (debug = true in the web.config) the ASP.NET AJAX product will use the commented and pretty formatted javascript code allowing you to better debug your application.

Server controls can be registered in the web.config instead of on each page they are used (AKA the @Register directive at the top of the ASP.NET page that gets generated when you add a custom control).  All you need to do is put a controls section in your web.config and add the tagPrefix section.  Search the web for an example (I didn’t get the example wrote down in time).

You can put server side comments into your ASP.NET pages.  The format is <%– Comment here –%>.  These are nice because they are not rendered out to the browser, they are stripped out by the ASP.NET engine during rendering.

Use the OutputCache attribute on pages to increase performance.  You can also do the same thing to individual controls as well.  If you are doing it with controls, look into the Shared attribute and ensure you have it set correctly for your application.

In ASP.NET 2.0, there is a Substitution control that allows you to cache everything but ‘this’ on the page.  You can have a cached page that only has one part of it dynamically rendered out.

You can use the web.config to specify common caching profiles so that multiple pages can share the settings.

If you need to update your ASP.NET application, you can take your site offline by simply dropping a App_Offline.htm file in the root directory of your site.  ASP.NET will see this and stop the ASP.NET engine and render the contents of this file.  This is a much more graceful way to perform public updates.  One thing to remember is to ensure it is at least 512 bytes long otherwise IE will override that file and show something from IE.  You never want that to happen.  So you can just pad the file with comments if needed.

Web deployment projects in VS 2005 can help ensure settings in your web.config are configured properly for deployment.  This is helpful for not leaving Debug=true in your web.config on your live site.  Even better though, you can set the machine.config to always override the debug settings on your production web server.  This ensures that a web site never runs in debug.  You do this by adding <deployment retail=true /> in the machine.config.

The ASP.NET AJAX javascript code is only 14.4 K – 20.4 K large.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: