Tip: Error Handling in .NET

In .NET development the common approach to exception handling is to make use of Try...Catch...Finally.

try {
  // some code
}
catch (NullReferenceException ex)
{
   // handle the exception
}

Sometimes you don't need to handle the exception and you just want to throw it to the calling application

try {
  // some code
}
catch (NullReferenceException ex)
{  
   throw ex;
}

But, if you do that you will get useless messages while debugging your application.

The better approach is to just call throw without "ex"...

try {
  // some code
}
catch (NullReferenceException)
{  
   throw;
}

This now provides the following error message which is much more useful.

Tags:

 

ASP.NET MVC Setup

First things fist... make sure you have the pre-requisites.  Start by removing any previous or beta versions of ASP.NET MVC.  I start with the ASP.NET MVC Downloads page.

Intall the ASP.NET MVC Framework 1.0 - the one you really need is AspNetMVC1.msi

A couple times an issue has come up where the MVC Project Template is not available when you go to New Project.

Double check that you have the project templates at this location...

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033

The MVC Project template is called MvcWebApplicationProjectTemplate.zip

If it is there and you are not getting it as an option try opening the Visual Studio Command Prompt and running the following commands to install the templates:

devenv.exe /installvstemplates
devenv.exe /setup

If you just want the starter project, you can download the following.

MvcStarter.zip (273.24 kb)

Tags:

 

Virtual path maps to another application

I use Web Deployment Projects to build an ASP.NET web site.  

When I built the web project by itself I got a successful build, but the Web Deployment Project received the following error...

The virtual path '/UserControls/Common/ModalProgress.ascx' maps to another application, which is not allowed.

I scratched my head for a few minutes then decided to try adding the ASP.NET root operator "~" in front of the path name.  So, the path became '~/UserControls/Common/ModalProgress.ascx'

After making this change I was able to build my Web Deployment Project.

For more info: Web Deployment Projects and ASP.NET Paths

Tags:

 

Things That Should Be Easy

There are certain tasks that a web developer will across many times, and that often take more time and thought than they should.  This is my list of items I often come across that I wonder why I don't have a better solution in place...

Grid Sorting and Paging

Client-side and server-side.  If the data is not too large handle all sorting and paging on the client.  Otherwise, write your back-end code to handle AJAX post-backs for each page of data.  To avoid special queries and stored procedures try populating an in-memory list and writing your sorting/paging routines on that list.

Validation

Gone is the day of drag-and-drop validation controls in ASP.NET.  The just cloud up your html pages.  Yes, they do server-side validation, but are you testing that like you should... probably not.  Validation can be wrapped up in client-side and server-side objects or classes.

 

Tags: