在MVC 5异常的Web API属性路线:对象尚未初始化。确保HttpConfiguration.EnsureInitialized() [英] Web API Attribute Routes in MVC 5 exception: The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized()
问题描述
在一个MVC 5使用Web API我有以下的,只使用属性的路由:
On an MVC 5 with Web API I have the following, using only Attribute Routes:
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" }); // TODO: Check for Apple Icons
RouteTable.Routes.MapMvcAttributeRoutes();
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
AreaRegistration.RegisterAllAreas();
在RouteTable所有的MVC路线创建...但不是API的...
In the RouteTable all the MVC routes were created ... But not the API ones ...
我检查了RouteTable.Routes,我看到一个异常:
I checked the RouteTable.Routes and I see an exception:
的对象还没有被初始化。确保HttpConfiguration.EnsureInitialized()所有其他初始化code后,被称为应用程序的启动code。
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()
在System.Web.Http.Routing.RouteCollectionRoute.GetEnumerator()
在System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() at System.Web.Http.Routing.RouteCollectionRoute.GetEnumerator() at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
有关测试此我只加两个Web API操作该项目:
For testing this I added only two Web Api actions to the project:
[RoutePrefix("api")]
public class StatApiController : ApiController {
[Route("stats/notescreateddaily"), HttpGet]
public IHttpActionResult NotesCreatedDaily() {
// Some code
}
[Route("stats/userscreateddaily"), HttpGet]
public IHttpActionResult UsersCreatedDaily() {
// Some code
}
}
我缺少的东西吗?
Am I missing something?
感谢您,
米格尔
Thank You, Miguel
推荐答案
解决方案实际上是在替换:
The solution is in fact replacing:
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
方式:
GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes());
这是网页API的变化2。
That was a change in Web API 2.
这篇关于在MVC 5异常的Web API属性路线:对象尚未初始化。确保HttpConfiguration.EnsureInitialized()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!