MVC 3 Web API路由不起作用

Jef*_*den 1 asp.net-mvc asp.net-mvc-3 asp.net-web-api asp.net-web-api-routing

我正在与MVC 3 Web API中的路由问题作斗争.看起来应该很简单,但我没有取得任何进展.

我的错误是:

  <Error>
     <Message>The request is invalid.</Message>
       <MessageDetail>
           The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'Boolean NewViolationsPublished(Int32)' in 'BPA.API.Controllers.CacheManagementController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
       </MessageDetail>
   </Error>
Run Code Online (Sandbox Code Playgroud)

我的RegisterRoutes是这样的:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
              name: "NukeAllItemsFromCache",
              routeTemplate: "api/CacheManagement/NukeAllItemsFromCache");


            routes.MapHttpRoute(
                 name: "ControllerAndAction",
             routeTemplate: "api/{controller}/{action}"
             );

            routes.MapHttpRoute(
                 name: "ControllerAndActionAndId",
                 routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional, action = "Get" },
              constraints: new { id = @"^\d+$" } // Only integers 
             );

            routes.MapHttpRoute(
                name: "ControllerAndId",
                routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional },
          constraints: new { id = @"^\d+$" } // Only integers 
            );
        }
Run Code Online (Sandbox Code Playgroud)

我的控制器是(我拿出代码以方便阅读):

 public class CacheManagementController : ApiController
    {
        public CacheManagementController()        

        [HttpGet]
        public bool NewViolationsPublished(int id)

        [HttpGet]
        public bool IsCached(CachedItemLabels label, int clientId)

        [HttpGet]
        public void RemoveItemFromCache(int clientId, CachedItemLabels cacheLabel, string test)

        [HttpGet]
        public string NukeAllItemsFromCache()    
    }
Run Code Online (Sandbox Code Playgroud)

我试图打电话时收到错误:

http://localhost/api/CacheManagement/NukeAllItemsFromCache
Run Code Online (Sandbox Code Playgroud)

TIA

Rya*_*n M 5

您确定要在正确的位置定义路线吗?应使用System.Web.Http.GlobalConfiguration.Configuration.Routes.MapHttpRoute(通常在App_Start中的WebApiConfig.cs文件中)配置Web API路由,如http://www.asp.net/web-api/overview所示/ extensibility/configured-aspnet-web-api,它与vanilla MVC使用的路由配置不同.

我想您可能正在使用默认的Web API路由,即api/{controller}/{id},ID可选.该动作由HTTP动词确定,在这种情况下是GET,并且所有方法都通过[HttpGet]匹配.

编辑:示例

Global.asax中

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalModifiers.ApplyGlobalConfiguration(this, true);
    }
Run Code Online (Sandbox Code Playgroud)

WebApiConfig.cs

public static class WebApiConfig
{
   public static void Register(HttpConfiguration config)
   {
        config.Routes.MapHttpRoute(
            name: "action-specific",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
    );
}
Run Code Online (Sandbox Code Playgroud)