Search code examples
asp.net-mvciis-7.5nullreferenceexceptionasp.net-routing

NullReferenceException on RouteTable.Routes


My ASP.NET MVC 3 web project was working fine, then I recompiled it and now I'm getting this error (some paths and namespaces changed to protect the innocent):

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:

Line 66:
RouteTable.Routes.MapRoute( Line 67:
"DefaultRoutes", // Route name Line 68:
"{module}/{controller}/{action}/{id}", // URL with parameters

Source File: ....\Global.asax.cs
Line: 66

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Project.MvcApplication.RegisterRoutes(RouteCollection routes) in D:.....\Global.asax.cs:66 Project.MvcApplication.Application_Start() in D:.....\Global.asax.cs:139

[HttpException (0x80004005): Object reference not set to an instance of an object.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +4051717 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +352 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375

[HttpException (0x80004005): Object reference not set to an instance of an object.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11642112
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4865877

I'm using IIS7.5 on Windows 7 x64, and the app pool is configured to run with .NET 4 and the 'Integrated' managed pipeline.

I've tried rebooting, and also cleaning out the bin and obj folders with no success.

Some extra information that turns out to be important - the line missing from the error page above:

RouteTable.Routes.MapRoute(
    "DefaultRoutes", // Route name
    "{module}/{controller}/{action}/{id}", // URL with parameters
    new { module = _loadedModules.FirstOrDefault().Name, controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Solution

  • This project dynamically loads modules and what wasn't clear from the original ASP.NET error page was that the NullReferenceException was being caused by the final line of the MapRoute() method call.

    It turns out that _loadedModules was empty, so FirstOrDefault() was returning null - hence the exception.