ASP.NET Ajax Error/Exception Handling – the simple way
A lot has been said about error/exception handling with ASP.NET Ajax pages, more specifically, how to properly handle and display error messages inside an UpdatePanel. For instance, Scott Gu wrote this sometime ago:
You can now optionally [...] intercept any error message sent back from the server, and perform custom client-side actions as a result [...]. (source)
Well, I disagree with “optionally”. As far as I understand, if you don’t explicitly intercept the error/exception sent back from the server from within an UpdatePanel, your user will get an unfriendly javascript error like this:
So what can you do to fix this? Easy, just add this piece of Javascript code to your Masterpage (or to any page if you don’t have/use a Masterpage):
<%-- This script must be placed after the form declaration (i.e. <form id="form1" runat="server">) --%>
<script type="text/javascript">
Sys.Application.add_load(AppLoad);
function AppLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
}
function EndRequest(sender, args) {
// Check to see if there's an error on this request.
if (args.get_error() != undefined) {
var msg = args.get_error().message.replace("Sys.WebForms.PageRequestManagerServerErrorException: ", "");
// Show the custom error.
// Here you can be creative and do whatever you want
// with the exception (i.e. call a modalpopup and show
// a nicer error window). I will simply use 'alert'
alert(msg);
// Let the framework know that the error is handled,
// so it doesn't throw the JavaScript alert.
args.set_errorHandled(true);
}
}
</script>
Now you should get error messages like this (javascript alert): 
This is exactly how exceptions used to be displayed when thrown inside an UpdatePanel before .NET 3.5.
Note: I do understand the benefits of a the new approach. It gives you flexibility to present the error message any way you want, before your hands where quite tied to this simple Javascript alert window.
As a matter of fact, I’ve implemented a nice ModalPopup and have ensure that only user-friendly messages are actually shown: only messages of a certain type (i.e. UIMessageException) are forwarded to the user without any cleanup/transformation.
References:
- Exception handling best practices in ASP.NET web applications
- How Do I: Customize Error Handling for the ASP.NET AJAX UpdatePanel
- MVP blog: UpdatePanel: having fun with errors
- MSDN post (be careful, not 100% correct): Customizing Error Handling for ASP.NET UpdatePanel Controls
Cheers!
ASP.NET Development server slow on Windows Vista/7 with Firefox or Chrome
While developing an ASP.NET website running it on the Visual Studio ASP.NET development server I was noticing that page loads exceedingly slowly in Firefox and Google Chrome after upgrading to Windows 7 (same issue occurs with Windows Vista).
A page refresh would usually take up to 3 seconds (localhost) even without changing the source code (so it should be instantaneously, especially now that I’m using a SSD).
It appears that there is some kind of bug on Vista/Windows 7 with DSN and IPv6, but that can be easily fixed. Here are some of the solutions I have found:
1. Recommended Solution – machine wide: uncomment the localhost address in the hosts file (%WINDIR%\System32\drivers\etc\hosts): (source)
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
# localhost name resolution is handled within DNS itself.127.0.0.1 localhost# ::1 localhost
2. Firefox-only solution: disable IPv6
1. Type about:config in the address bar and press Enter.
2. Scroll down until you find network.dns.disableIPv6.
3. Double-click on it to change its value to true.
4. Restart Firefox.
3. System wide-configuration (option 1): Disable IPv6 Random identifier
netsh interface tcp set global autotuninglevel=disabled
4. System wide-configuration (option 2): Disable IPv6 from Your LAN Interfaces and Connections
1. Launch Vista, click on Start, and then click on Run. Once the Run window appears, type regedit.
2. Once you have accessed the registry, you will add a registry value as follows: (DWORD type) Set to OxFF.
3. The registry is as follows: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\DisabledComponents
Speeding up build times in ASP.NET with RamDisk
Here is another quick tip on how to speed up build times on your ASP.NET website projects: create a ram-based drive (i.e. using RamDisk) and change your default “Temporary ASP.NET Files” to this memory-based drive.
Then every time you change your ASP.NET pages (or any code-behind or reference DLLs) the build will happen much faster because is doing all in memory.
So, how you do this? Simply follow these steps:
1) Make sure you have enough free memory:
Open the usual apps you use (i.e. Visual Studio, Outlook, etc) and check how much free memory you have (Task Manager -> Performance -> Physical Memory -> Available).
So, let’s say you have 500MB free and your total memory physical memory is 2GB (Task Manager -> Performance -> Physical Memory -> Total). The total I’d recommend allocating for your virtual drive would be half of your free memory, or 250MB.
If you’re short on free memory, try running some cleanup tools like TuneUp Utilities (very good, but paid) or CCleaner (free). Besides that, you can always check what services are running and stop/disable the ones you really don’t need (this is a delicate step, be careful not to stop essential services – use Google to find out what’s essential and what’s not).
2) Install RamDisk
I’m using a free implementation that can be found here. You can follow these steps for a details explanation of this tool and installation instructions.
Make sure you install it and create a drive letter (i.e. “R:”) with the disk size amount calculated in the previous step. If you over-allocate it, your Windows will run out of memory and the whole purpose of this idea will be worthless.
3) Change your default “Temporary ASP.NET Files” folder to your new ram-based drive
Here you have two options. You can do this only for a specific site or for all projects running on your computer. If you have too many sites you may run out of disk space in your ramdisk, so choose your option wisely
3.1) Configuration for a specific website (local settings):
Change your site’s web.config “compilation” property to specify the new path. Below is an example that assumes “R:\ASP_NET_TempFiles\” as the new ram-based folder location:
<system.web>
....
<compilation debug="true" tempDirectory="R:\ASP_NET_TempFiles\">
....
</compilation>
....
</system.web>
3.2) Configuration for all ASP.net websites (global settings):
To implement this globally all you need to do is change your global web.config (which is usually located somewhere like this C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config)and adjust the compilation properties the same way the local settings (previous example):
<system.web>
....
<compilation debug="true" tempDirectory="R:\ASP_NET_TempFiles\">
....
</compilation>
....
</system.web>
And your done!
Two last tips I can give you is to keep an eye on the usage of your ramdisk folder. If it get’s full, you’ll have to manually delete some old temporary files. The second tip is to watch how your free memory, if it gets too close to it’s total it might indicate the you either have too much programs running or you might have to reduce your ramdisk size.
Happy speed building!
Tip/Trick: Optimizing ASP.NET Build Time with Dynamic Compilation (optimizeCompilations=”true”)
If you are experiencing slow builds with ASP.NET projects or want to learn how to speed them up please read on.
Microsoft has introduced a new optimizeCompilations switch in ASP.NET that can greatly improve the compilation speed in some scenarios. You can read this blog post to understand the overall idea, study a more detailed explanation about Dynamic Compilation here or simply follow these 2 easy steps:
1) Install this hot-fix:
- Windows XP: http://code.msdn.microsoft.com/KB969612
- Windows Vista: http://code.msdn.microsoft.com/KB967535
- Windows 7 – not necessary
2) Update your web.config as follows:
Add optimizeCompilations=”true” to your <compilation …> tag. Below is an example:
<system.web>
....
<compilation debug="true" optimizeCompilations="true">
....
</compilation>
....
</system.web>
And you’re done! Next time you change something inside the App_code or even a dll inside the bin folder you might not have to wait the entire site to rebuild.
Important note: this approach speeds things up but you might get some weird errors when you start changing some code signatures or restructuring dlls.In this case, all you need to do is a simple “rebuild project/solution” inside your Visual Studio to get things straight again.
How To Deploy an ASP.NET web application using Team Foundation Server
Here is a quick tip on how to modify a TFS build script to publish an ASP.NET site to an existing IIS server (i.e. development environment).
Sample Code
Assuming you have created your basic TFSBuild.proj file, add this to the bottom of the file (right before the </project> tag):
<!– This task will be executed after the build is copied to the drop location (i.e. build storage) and
will take care of automating the deployment of the project to an IIS folder
by ‘Wagner Danda da Silva’
–>
<Target Name=”AfterDropBuild” Condition=” ‘$(BuildBreak)’!=’true’ “>
<!– Let’s prepare the copy by setting the drop location (IIS website folder) ** –>
<CreateProperty Value=”\\dswebdev\QA\“> <!– replace this with your IIS folder –>
<Output TaskParameter=”Value” PropertyName=”MyDropLocation”/>
</CreateProperty>
<!– The path below is where the sources can be found after building ASP.NET projects, you probably won’t need to edit this. –>
<CreateItem Include=”$(DropLocation)\$(BuildNumber)\%(ConfigurationToBuild.FlavorToBuild)\_PublishedWebsites\Web\ \*”>
<Output TaskParameter=”Include” ItemName=”MyDropFiles”/>
</CreateItem>
<!– Let’s remove any pre-deployed files. This will delete any existing files inside all folder and subfolders of the drop location. I opted not to use the default TFS delete task because that was removing security permission on my folders and messing up with my build. The command below won’t do that, will simply delete old files (but it won’t remove the folders, so if you deleted a folder on your source code you’ll have to manually remove it from the drop location – if you actually care for that
–>
<Exec Command=’del /f /s /q $(MyDropLocation)\*’/>
<!– Copy the files from the build drop location to the IIS website folder –>
<Exec Command=’xcopy /Y /R /E “$(DropLocation)\$(BuildNumber)\%(ConfigurationToBuild.FlavorToBuild)\_PublishedWebsites\Web\.” “$(MyDropLocation)\."’ />
</Target>
More details can be found here:http://tfsdeploy2iis.codeplex.com/
