Asp.net web api 简单WebApi路由问题

Asp.net web api 简单WebApi路由问题,asp.net-web-api,asp.net-web-api-routing,Asp.net Web Api,Asp.net Web Api Routing,我正在尝试让一个简单的WebAPI项目工作 我有一个WebApiConfig.cs,上面有: config.Routes.MapHttpRoute( name: "cap-getvehicleoptions", routeTemplate: "api/{controller}/{action}/{type}/{capid}", defaults: new { controller="Cap", action="GetVehicleOption

我正在尝试让一个简单的WebAPI项目工作

我有一个WebApiConfig.cs,上面有:

config.Routes.MapHttpRoute(
         name: "cap-getvehicleoptions",
         routeTemplate: "api/{controller}/{action}/{type}/{capid}",
         defaults: new { controller="Cap", action="GetVehicleOptions" }
     );
然后该控制器:

 public class CapController : ApiController
    {
    public string GetVehicleOptions(string type, int capid)
    {
        var response = CapService.GetVehicleOptions(type, capid);
        response.Url = string.Format("/api/cap/getvehicleoptions/{0}/{1}", type, capid.ToString());
        return Serialization.Serialize(response);    
    }
}
当我运行时,请尝试以下操作:

我得到:

{“Message”:“未找到与请求URI“”匹配的HTTP资源。”,“MessageDetail”:“未在与请求匹配的控制器“Cap”上找到任何操作。”}

我想知道这是否是因为int-capid属性,但尝试了字符串,但没有帮助


谢谢

检查配置是否正确: 您在WebApiConfig.cs中的路由是否在Register方法中

您的WebApiConfig是否已添加到GlobalConfiguration

Global.asax中的示例:

  protected void Application_Start()
   {
       AreaRegistration.RegisterAllAreas();
       GlobalConfiguration.Configure(WebApiConfig.Register);
       //...
    }

检查您是否正确配置了它: 您在WebApiConfig.cs中的路由是否在Register方法中

您的WebApiConfig是否已添加到GlobalConfiguration

Global.asax中的示例:

  protected void Application_Start()
   {
       AreaRegistration.RegisterAllAreas();
       GlobalConfiguration.Configure(WebApiConfig.Register);
       //...
    }

在路由模板中,URI中包含操作名称。在这种情况下,使用属性指定允许的HTTP方法


用[HttpGet]属性修饰操作,它应该可以工作。

在路由模板中,操作名称包含在URI中。在这种情况下,使用属性指定允许的HTTP方法

用[HttpGet]属性修饰操作,它应该可以工作