0

我在 PersonController 类中有以下功能

    [HttpGet]
    [ActionName("GetBloggersNotFollowed")]
    public IQueryable<object> GetBloggersNotFollowed(int companyId)
    {
        return Uow.People.GetPeople().Select(p => new { p.Email, p.FirstName, p.LastName, p.PhoneNumber, p.Type, p.UserName, p.Country, p.Id });
    }

它用于检索人员列表。

我这样称呼这个函数

$.ajax({ 
   url: "/api/person/GetBloggersNotFollowed/1" 
}).success(function (people) { 
     PersonModule.GetPeople(people); 
});

我已经在我的WebApiConfig.cs

config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

当我尝试在浏览器中调用路由时出现错误

<Error>
 <Message>
  No HTTP resource was found that matches the request URI 'http://localhost:1045/api/person/GetBloggersNotFollowed/1'.
  </Message>
  <MessageDetail>
       No action was found on the controller 'Person' that matches the request.
  </MessageDetail>
</Error>

我不知道我错了。任何人都可以看到问题吗?

4

2 回答 2

1

参数名称对路由匹配很重要。

id您已在路由中命名参数,但方法将其命名为companyId.

{id}将路由更改为或{companyId}companyId参数更改为id

于 2012-12-28T17:19:58.417 回答
1

将您的路线替换为此路线:

config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{companyId}",
            defaults: new { id = RouteParameter.Optional }
        );

这个答案补充了马克琼斯的答案。

于 2012-12-28T17:25:01.917 回答