2017-06-16 18 views
5

我正在測試Web Core API,並且出現500內部服務器錯誤。 我的控制器上的方法是;InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException

[Route("api/property")] 
public class PropertyController : BaseSirController 
{ 

    [HttpPost("{contractId}/saveproperty")] 
    public IActionResult SaveProperty(int contractId, [FromBody] PropertyContractDto property) 
    { 
     if (property == null) 
     { 
      return BadRequest(); 
     } 

     if (contractId == 0) 
     { 
      return NotFound(); 
     } 

     return Ok(); 
     //return CreatedAtRoute("GetPropertyInspectionsForContract", new {contractId = contractId}, properties); 
    } 

,在我的PowerShell命令我輸入:

Invoke-RestMethod http://localhost:17972/api/property/7/saveproperty -Method POST -Body (@{UPRN="1244"; StreetName="The Street"; AddressLine2="ALine2"; AddressLine3="ALine3"; AddressLine4="ALine4"; Postcode="N1 7PL"; Latitude="23"; Longitude="77"; BlockUPRN="B1244"; PropertyNo="23"; BlockName="Wenlock Court"; Comments="blah blah blah"; PropertyTypeId="1"; NumberOfBathrooms="1"; NumberOfBedrooms="2"; NumberOfKitchens="1"; PropertyContractId="23"; PropertyType="Maisonette"} | ConvertTo-Json) -ContentType "application/json" 

我得到該錯誤消息;

Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error. 
At line:1 char:1 
+ Invoke-RestMethod http://localhost:17972/api/property/7/saveproperty -Method POS ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 

我無法解決如何解決這個問題。

+1

鑑於服務器代碼是多麼簡單我會猜測這是一個參數問題。首先,我看不到你要發送'contractId'的地方,其次從一些快速閱讀我不相信'PropertyContractDto'對象正在正確傳遞。這不完全是我的專業領域,但這正是我期待的目標。 –

+0

contractId是7,它通過OK。問題是在[FromBody]屬性之後發送有效負載。我不知道爲什麼這是一個問題。 – arame3333

+0

啊我現在看到了。您是否測試過將JSON轉換爲PropertyContractDto類? –

回答

2

我發現問題出在我的Startup.cs,ConfigureServices方法中。如果我把它放在services.AddMvcCore().AddJsonFormatters();它的工作