好的,我花了一整天时间查看这段代码,无法弄清楚我做错了什么。
这是.NET 4.5.2上的Web API 2 这是我的Global.asax.cs
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
new AutoFacContainer(GlobalConfiguration.Configuration, Assembly.GetExecutingAssembly());
GlobalConfiguration.Configuration.Services.Replace(typeof (IHttpControllerSelector),
new VersionHttpControllerSelector(GlobalConfiguration.Configuration));
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configure(WebApiFilter.Register);
}
}
这是我的WebApi.Config
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
}
}
我只想使用路由属性,所以我没有配置任何手动路由。
最后,这是我的'默认控制器'
[RoutePrefix("test")]
public class DefaultController : WebApiBaseController
{
[HttpGet]
[Route("")]
public virtual IHttpActionResult GetRoot()
{
return Ok("This is Version1 of GetRoot");
}
[HttpGet]
[Route("{test/{id:int}", Order = 1)]
public virtual IHttpActionResult GetTest(int id)
{
return Ok("This is Test1 in Version1");
}
}