asp.net-mvc – MVC 5中的Web API属性路由异常:该对象尚未初始化.确保HttpConfiguration.EnsureInitialized()

在带有Web API的MVC 5上,我有以下内容,仅使用属性路由:
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那些……

我检查了RouteTable.Routes,我看到了一个异常:

该对象尚未初始化.确保在所有其他初始化代码之后在应用程序的启动代码中调用HttpConfiguration.EnsureInitialized().

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动作:

[RoutePrefix("api")]
public class StatApiController : ApiController {

  [Route("stats/notescreateddaily"),HttpGet]
  public IHttpActionResult NotesCreatedDaily() {
    // Some code
  }

  [Route("stats/userscreateddaily"),HttpGet]
  public IHttpActionResult UsersCreatedDaily() {
    // Some code
  }

}

我错过了什么吗?

谢谢,
米格尔

解决方法

解决方案实际上取代了:
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

通过:

GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes());

这是Web API 2的变化.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....