0

我的ASP.NET MVC 3 web項目工作正常,然後重新編譯我它,現在我得到這個錯誤(一些路徑和名稱空間更改爲保護無辜者):的NullReferenceException上RouteTable.Routes

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

我在Windows 7 x64上使用IIS7.5,並將應用程序池配置爲與.NET 4和「集成」託管管道一起運行。

我試過重新啓動,也清理出bin和obj文件夾沒有成功。

這原來是很重要的一些額外的信息 - 該行從上述錯誤頁面丟失:

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 
); 
+0

你能發佈你的路線嗎?它可能會有所幫助...... –

回答

0

這項目動態加載模塊,並且從原始ASP.NET錯誤頁面中不清楚的是NullReferenceException是由MapRoute()方法調用的最後一行導致的。

事實證明_loadedModules是空的,所以FirstOrDefault()返回null - 因此是例外。

1

在你的global.asax.cs,你可以這樣寫

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapRoute(....);//you should use routes parameter rather than RouteTable.Routes 
} 
+0

這是關於使用參數的一個好處 –

相關問題