我正在Xamarin.Forms(共享項目庫)中開發一個應用程序,它向本地託管的API發送自定義類型列表(只是文檔編號和文件類型枚舉)。錯誤500發送JSON POST請求到API
如果我捕捉到JSON字符串,並從郵差送它,一切工作正常,但只要我從應用程序運行httpClient.PostAsync,我收到以下錯誤:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: no-cache Pragma: no-cache Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 25 May 2017 16:00:44 GMT Content-Type: application/json; charset=utf-8 Expires: -1 Content-Length: 36 }}
我不知道我做錯了什麼。任何人都可以幫忙嗎?
類型:
class EmailFiles
{
public string docNumber { get; set; }
public EmailFileTypes.EmailFileType type { get; set; }
}
請求:
List<EmailFiles> files = new List<EmailFiles>();
if (chkShipping.IsToggled)
{
EmailFiles file = new EmailFiles();
file.type = EmailFileTypes.EmailFileType.CustomerFile;
file.docNumber = Helpers.GlobalVariables.gSOLookup.PackList;
files.Add(file);
}
if (chkClosed.IsToggled)
{
EmailFiles file = new EmailFiles();
file.type = EmailFileTypes.EmailFileType.ClosedFile;
file.docNumber = Helpers.GlobalVariables.gSOLookup.Invoice;
files.Add(file);
}
if (chkInvoice.IsToggled)
{
EmailFiles file = new EmailFiles();
file.type = EmailFileTypes.EmailFileType.Invoice;
file.docNumber = Helpers.GlobalVariables.gSOLookup.Invoice;
files.Add(file);
}
string url = SetUrls.urlMtApi + "/api/EmailFile/?emailAddresses=" + strEmails;
string strJson = JsonConvert.SerializeObject(files);
//Results in: "[{\"docNumber\":\"234273\",\"type\":1},{\"docNumber\":\"633007\",\"type\":2}]" - which works in Postman!!
StringContent content = new StringContent(strJson, Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.PostAsync(url, content);
Web服務:
[System.Web.Http.HttpPost]
public string EmailFile([FromBody]string jsonfiles, [FromUri] string emailAddresses)
{
List<EmailFiles> files = JsonConvert.DeserializeObject<List<EmailFiles>>(jsonfiles);
...
}
您需要調試500錯誤服務器端。這意味着服務器在某處引發異常。 –