Web API中的路由/操作已损坏

时间:2013-04-17 16:56:32

标签: c# c#-4.0 asp.net-web-api asp.net-web-api-routing

我在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);
    }

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

任何想法?

请求网址:http://localhost/api/api/Users

2 个答案:

答案 0 :(得分:4)

此处参数'id'是可选的,但操作选择器期望在操作上为其指定默认值。

public IEnumerable Get(string id = null ,int page,int offset)

另外关于网址,可能是拼写错误,你的意思是http://localhost/api/Users而不是http://localhost/**api/api**/Users

答案 1 :(得分:0)

您尚未在路线中指定行动。您需要在网址中添加{action}条,或在default对象中指定操作。