2016-04-21 106 views
0

我正在將方法添加到其他Web API控制器。測試現有的一種方法,我可以打破我的中斷點。然而,如果我嘗試調用其中一個新的,我會得到一個404。我正在使用IIS Express和Postman進行本地測試。下面的例子,任何想法可能會導致這種情況?Web API控制器不識別新添加的方法

當我嘗試調用新端點,這是響應我收到:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53453/api/nisperson/addnewconnection'.", 
"MessageDetail":"No action was found on the controller 'NISPerson' that matches the request."} 

現有方法:

[HttpPost] 
[ActionName("register")] 
public ClientResponse PostRegisterPerson(HttpRequestMessage req, PersonModel person) 
{ 
    // This method is getting hit if I call it from Postman 
} 

端點:http://localhost:53453/api/test/register

新增方法:

[HttpPost] 
[ActionName("addnewconnection")] 
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName) 
{ 
    // This is the new method which when called from Postman cannot be found. 
} 

端點:

[HttpPost] 
[ActionName("addnewconnection")] 
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName) 
{ 
    // This is the new method which when called from Postman cannot be found. 
} 

的路由處理不能映射這個URI:http://localhost:53453/api/test/addnewconnection

+0

您在「addnewconnection」附近缺少「」「 –

+0

在實際的代碼中添加了引號。我在向StackOverflow添加問題時做了一個類型。 –

+0

在聲明路由時,我通常使用完全合格的路由 [ActionName(「ReIndex」)] [Route(「api/autocomplete/reindex」)] –

回答

2

你在你的方法簽名(EmailFirstNameLastName)定義了三個需要參數:http://localhost:53453/api/test/addnewconnection對你的方法,因爲你沒有提供這三個要求編輯參數。

正確URI(離開你的方法,是)實際上是以下各項之一:

http://localhost:53453/api/test/addnewconnection?Email=foo&FirstName=bar&LastName=baz 

要麼提供這些參數的URI內部,如圖或者將它們作爲不要求提供一個默認值:

[HttpPost] 
[ActionName("addnewconnection")] 
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email = null, String FirstName = null, string LastName = null) 
{ 
    // This is the new method which when called from Postman cannot be found. 
} 

提供默認值將允許您使用原始URI訪問您的方法。

+0

謝謝!我很習慣傳遞Model對象,但我沒有意識到這是簡單類型所需要的。 –

1

原因是它期望其他參數(簡單的字符串類型)在你沒有提供的查詢字符串中提供。所以,它試圖用單個參數調用Post並且無法找到它。簡單類型默認從URI讀取。如果您希望他們從表單主體讀取,請使用FromBody屬性。