Fil*_* DO 139 .net c# asp.net-mvc-routing
我安装了Visual Studio 2013,当我运行我的应用程序时,我收到以下错误.
我不知道我要在哪里初始化这个对象.
该怎么办?
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
Run Code Online (Sandbox Code Playgroud)
gen*_*ane 231
如果你在Application_Start结束时这样做,那就太晚了,因为已经调用了WebApiConfig.Register.
解决此问题的最佳方法是通过替换Global.asax来使用新的初始化方法:
WebApiConfig.Register(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)
通过
GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)
Ian*_*cer 137
请参阅下面的 @ gentiane的答案,了解正确处理此问题的方法.
在尝试添加的Application_Start方法结束时Global.Asax.cs: -
GlobalConfiguration.Configuration.EnsureInitialized();
Run Code Online (Sandbox Code Playgroud)
Jef*_*tes 66
当我在WebApi中使用属性路由时,我实际上遇到了这个错误.
我有
〔路线( "的WebAPI/siteTypes/{siteTypeId"]
代替
〔路线( "的WebAPI/siteTypes/{siteTypeId}"]
为我的路线,并得到此错误.我只是错过了最后的花括号.一旦我将其重新添加,此错误就不会再次发生.
小智 29
这是旧的,但是在搜索此错误时谷歌的第一个结果.经过相当多的挖掘后,我能够弄清楚发生了什么.
tldr:
所有GlobalConfiguration.Configure都调用你的动作并调用EnsureInitialized(). config.MapAttributeRoutes()之前必须叫EnsureInitialized()因为EnsureInitialized只运行一次.
含义:如果你来自现有的Mvc项目,你所要做的就是:
- 添加 GlobalConfiguration.Configuration.EnsureInitialized(); 到Application_Start方法的底部.
- 将整个配置移动到对GlobalConfiguration.Configure的单个调用:
GlobalConfiguration.Configure(config =>
{
WebApiConfig.Register(config);
config.MapAttributeRoutes();
...
});
Run Code Online (Sandbox Code Playgroud)
HttpConfiguration.Configuration有一个"Initializer"属性定义如下:
public Action<HttpConfiguration> Initializer;
Run Code Online (Sandbox Code Playgroud)
HttpConfiguration.EnsureInitialized()运行此操作并将_initialized设置为true
public void EnsureInitialized()
{
if (_initialized)
{
return;
}
_initialized = true;
Initializer(this);
}
Run Code Online (Sandbox Code Playgroud)
HttpConfiguration.MapAttributeRoutes调用内部方法AttributeRoutingMapper.MapAttributeRoutes设置HttpConfiguration.Initializer
public static void MapAttributeRoutes(...)
{
RouteCollectionRoute aggregateRoute = new RouteCollectionRoute();
configuration.Routes.Add(AttributeRouteName, aggregateRoute);
...
Action<HttpConfiguration> previousInitializer = configuration.Initializer;
configuration.Initializer = config =>
{
previousInitializer(config);
...
};
}
Run Code Online (Sandbox Code Playgroud)
GlobalConfiguration.Configure在调用您的操作后立即运行EnsureInitialized:
public static void Configure(Action<HttpConfiguration> configurationCallback)
{
if (configurationCallback == null)
{
throw new ArgumentNullException("configurationCallback");
}
configurationCallback.Invoke(Configuration);
Configuration.EnsureInitialized();
}
Run Code Online (Sandbox Code Playgroud)
不要忘记,如果你碰壁,可以访问http://aspnetwebstack.codeplex.com/SourceControl/latest获取asp.net的源代码.
对我来说,问题是我试图在我的路线中使用命名参数查询字符串字段:
[Route("my-route?field={field}")]
public void MyRoute([FromUri] string field)
{
}
Run Code Online (Sandbox Code Playgroud)
查询字符串字段自动映射到参数,实际上不是路由定义的一部分.这有效:
[Route("my-route")]
public void MyRoute([FromUri] string field)
{
}
Run Code Online (Sandbox Code Playgroud)
如果此错误似乎“无处不在”,即您的应用程序在一段时间内运行良好,请问问自己:在看到此错误之前,我是否向控制器添加了操作或更改了任何路由?
如果答案是肯定的(并且可能是),那么您可能在此过程中犯了错误。格式不正确、复制/粘贴操作以及忘记确保端点名称是唯一的,等等都会让您到此为止。此错误对如何解决它提出的建议可能会让您大吃一惊。
尽管如果未设置incase则上述答案有效,但就我而言,此材料已设置好。所不同的是,对于我编写的一个API,我给路由添加了/前缀。例
[Route("/api/abc/{client}")]
Run Code Online (Sandbox Code Playgroud)
更改为
[Route("api/abc/{client}")]
Run Code Online (Sandbox Code Playgroud)
为我修复