我已经安装了Visual Studio2013,当我运行我的应用程序时,我得到了下面的错误。
我不知道在哪里初始化这个对象。
怎么办?
Server Error in '/' Application.
The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
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.InvalidOperationException: The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
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:
[InvalidOperationException: The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.]
System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() +101
System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) +63
System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext) +107
System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +233
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +60
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
这是给AlumCloud的
现在正确的处理方法见下面@gentianee的回答。
在
GlobalConfiguration.Configuration.EnsureInitialized();
如果在Application_Start结束时执行,就太晚了,因为webapiconfig.register已经被调用。
解决此问题的最佳方法是使用新的初始化方法,方法是在global.asax中替换:
WebApiConfig.Register(GlobalConfiguration.Configuration);
被
GlobalConfiguration.Configure(WebApiConfig.Register);
实际上,当我在WebAPI中使用属性路由时,我遇到了这个错误。
我有
[路由(webapi/sitetypes/sitetypeid)]
而不是
[路由(webapi/sitetypes/siteTypeId)]
并得到了这个错误。我只是漏掉了最后的花括号。一旦我把它添加回来,这个错误就不会再发生了。