破碎路由/动作

问题描述:

我在WebApiConfig.cs定义我的默认路由:破碎路由/动作

 config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional, page = 20, offset = 0 } 
     ); 

,并在我的控制,我有一个动作:

// GET api/users 
    [HttpGet] 
    public IEnumerable<User> Get(string id, int page, int offset) 
    { 
     return id != null 
      ? new User[]{Get(id)} 
      : _userService.All().Skip(offset*page).Take(page); 
    } 

我知道这是最近工作,但现在我得到臭名昭着的“没有行动被发现在控制器'用户'匹配请求”错误。我似乎无法弄清楚什么(如果有的话)改变了。自从添加页面/偏移量的默认值以来,我已经取消了所有更改。

有什么想法?

请求URL:http://localhost/api/api/Users

+0

你'url'的样子对于这个动作? –

+0

@Conong http http:// localhost/api/api/Users – earthling

这里参数'id'是可选的,但动作选择器期望为动作指定一个默认值。

公共IEnumerable的获取(字符串ID = NULL,INT页,诠释偏移)

另外,关于链接,可能是一个错字,你的意思是http://localhost/api/Users而不是http://localhost/**api/api**/Users

+0

将路由def更新为'id =“”'而不是'id = RouteParameter.Optional'好像又让它工作了。现在我需要看看结果可能会破坏什么。 – earthling

您还没有指定的路线的动作。您需要将{action}作品添加到您的网址中,或者在default对象中指定动作。

+0

不太可能如图所示的注册是[默认](http://www.asp.net/web-api/overview/web-api-routing-and- action-route-in-aspnet-web-api)一个用于Web-API(注意'MapHttpRoute' - 不是'MapRoute') –

+0

@AlexeiLevenkov:你还需要一个动作。 WebAPI的默认路由也包含一个。 –

+0

从来没有在路线中的行动。这是mvc 4 web api项目默认设置的。 – earthling