Asp.Net Request routing implementation fails with Null Reference exception

I recently had the pleasure of working on the implementation of Asp.Net Request Routing feature that comes in bundled with .Net Framework 3.5 SP1. This is a cool stuff wherein care has been taken to ensure it can be used independently of MVC Framework. It can be used with Dynamic Data as well as implemented for any generic Asp.Net 2.0 application for Request routing functionality.

*Remember this is different from ASP.NET 2.0's URL Mapping.

The aim of this post is so obvious for many but I am writing this post because I couldn't get any pointers on this over the Internet, although I know I should have checked the end resolution in the very beginning; silly of me.


We were getting the following exception when we we were trying to route a request. Exception was being thrown from within our custom class implementing IRouteHandler.

Server Error in '/' Application. 
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Collections.Generic.Dictionary`2.GetEnumerator() +38
   Mycustomdll.Net.RouteHandler`1.GetHttpHandler(RequestContext requestContext) in C:\abcdedfgh\myprojectforfun\asdfsdf\Net\RouteHandler.cs:37
   System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +106
   System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +80
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

This application was failing for one of my customers in his environment. I was finally able to reproduce this problem locally on one of my machines as well. Found that ideally this should work fine if you have the latest build.

On debugging found it was failing at the section shown below in the custom Routehandler class while trying to iterate through the RouteValueDictionary collection even though there were items in the collection. It was failing with the null reference exception.

public class RouteHandler:IRouteHandler
       public IHttpHandler GetHttpHandler(RequestContext requestContext)
             foreach (var value in requestContext.RouteData.Values) <------------
             return (Page)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page));

You may see this crash behavior if you are running with the build version of System.Web.routing as shown below. I assume this is from the ASP.Net 3.5 SP1 Beta release, not sure. 


In order to rectify this issue ensure you move to the latest build for System.Web.Routing.dll as well as for System.Web.Abstractions.dll.


Lesson: Never try to run Beta/CTP version etc. on the production environment. Ensure you have the latest bits deployed.


Comments (3)

  1. Rahul Soni says:

    Good good! Must be a tough one to crack! How did you get to

    foreach (var value in requestContext.RouteData.Values) <————

    Did you enable Source code debugging in VS2008 to get to this?

  2. Saur212 says:

    Hey Rahul, hope you are recuperating well.

    Yeah, it was a tough one since it occurred randomly on systems even on machines which had the latest version, the dll was getting loaded from the bin folder instead!!.

Skip to main content