RouteTable.Routes 上的 NullReferenceException

发布于 2024-11-17 19:54:15 字数 2124 浏览 7 评论 0原文

我的 ASP.NET MVC 3 Web 项目工作正常,然后我重新编译它,现在我收到此错误(更改了一些路径和命名空间以保护无辜者):

对象引用未设置为 对象的实例。描述:安 期间发生未处理的异常 当前网络的执行 要求。请检查堆栈跟踪 有关错误的更多信息 以及它在代码中的起源。

异常详细信息: System.NullReferenceException:对象 未设置对实例的引用 对象。

来源错误:

第 66 行:
RouteTable.Routes.MapRoute( 第 67 行:
"DefaultRoutes", // 路线名称 Line 68:
"{模块}/{控制器}/{动作}/{id}", // 带参数的 URL

源文件:....\Global.asax.cs
线路:66

堆栈跟踪:

[NullReferenceException:对象 未设置对实例的引用 对象。]
项目.MvcApplication.RegisterRoutes(RouteCollection 路线)在 D:.....\Global.asax.cs:66 项目.MvcApplication.Application_Start() 在 D:.....\Global.asax.cs:139

[HttpException (0x80004005):对象 未设置对实例的引用 对象。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext 上下文,HttpApplication 应用程序)+4051717 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext、HttpContext 上下文、 MethodInfo[] 处理程序)+191
System.Web.HttpApplication.InitSpecial(HttpApplicationState 状态、MethodInfo[] 处理程序、IntPtr appContext、HttpContext 上下文)+352 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext、HttpContext 上下文)+407 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr 应用上下文)+375

[HttpException (0x80004005):对象 未设置对实例的引用 对象。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext 上下文)+11642112
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext 上下文)+141
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext 上下文)+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
);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-11-24 19:54:15

在你的 global.asax.cs 中你可以这样写

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

In your global.asax.cs you can write like this

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(....);//you should use routes parameter rather than RouteTable.Routes   
}
伴梦长久 2024-11-24 19:54:15

该项目动态加载模块,从原始 ASP.NET 错误页面中不清楚的是 NullReferenceException 是由 MapRoute() 方法调用的最后一行引起的。

事实证明 _loadedModules 是空的,因此 FirstOrDefault() 返回 null - 因此出现异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文