2012-12-28 26 views
0

我已經在PersonController類下面的函數ASP.Net的Web API定製的路線不工作

[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> 

我不知道我在這裏我錯了。任何人都可以看到問題嗎?

回答

1

參數的名稱對路徑匹配很重要。

您已在路線中命名參數id,但該方法將其作爲companyId

要麼將​​路線中的{id}更改爲{companyId},要麼將companyId參數更改爲id

+0

你是英雄我的朋友 –

+0

@WesleySkeen歡迎您 - 感謝您報告(p.s.請標記爲已接受,如果它回答了您的問題)。 –

1

更換你你的路線,這一個:

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

這個答案補充馬克·瓊斯的答案。

+0

對不起馬特馬克先到這裏。你的答案也有效。 –

+0

@WesleySkeen:沒問題,分享知識更重要,不是嗎? –