【发布时间】:2018-05-24 09:15:00
【问题描述】:
我已经习惯了使用 AspNetCore 2.0。
我有一个 WPF 客户端,它调用我的 Web api 控制器。
首先这是我的 Web Api(在 core 2.0 下)。
[Route("api/[controller]")]
public class EmailRecordController : Controller
{
[HttpPost]
[Route("ValidateEmail")]
public EmailRecord ValidateEmail([FromBody] EmailRecord emailRecord)
{
return _externalApiEmailService.Search(emailRecord.email);
}
}
我从我的 WPF 客户端调用它:
var uriController = "api/EmailRecord/ValidateEmail";
public async Task<EmailRecord> SearchEmail(
string emailAddress, string uriController)
{
//var uri = "http://localhost:49627//api/Email/Search";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:49627/");
var emailRecord = new EmailRecord {email = emailAddress};
var jsonInput = JsonConvert.SerializeObject(emailRecord);
var contentPost = new StringContent(jsonInput, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uriController, contentPost).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<EmailRecord>(json);
}
}
我的客户模型是:
public class EmailRecord
{
public string CompanyRef { get; set; }
public string ClientRef { get; set; }
public string email { get; set; }
public string did_you_mean { get; set; }
public string user { get; set; }
public string domain { get; set; }
public string format_valid { get; set; }
public string mx_found { get; set; }
public string smtp_check { get; set; }
public string catch_all { get; set; }
public string role { get; set; }
public string disposable { get; set; }
public string free { get; set; }
public string score { get; set; }
}
我的 asp.net.core 服务器上的模型是:
public class EmailRecord
{
public string CompanyRef { get; set; }
public string ClientRef { get; set; }
public string email { get; set; }
public string did_you_mean { get; set; }
public string user { get; set; }
public string domain { get; set; }
public string format_valid { get; set; }
public string mx_found { get; set; }
public string smtp_check { get; set; }
public string catch_all { get; set; }
public string role { get; set; }
public string disposable { get; set; }
public string free { get; set; }
public string score { get; set; }
}
当它被调用时,我得到这个:
- 响应 {StatusCode:500,ReasonPhrase:“内部服务器错误”,版本:1.1,内容:System.Net.Http.StreamContent,标头: { X-SourceFiles: =?UTF-8?B?RDpcREVWLUluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXGFwaVxFbWFpbFJlY29yZFxWYWxpZGF0ZUVtYWls?= 日期:2017 年 12 月 10 日星期日 19:58:02 GMT 服务器:红隼 X-Powered-By: ASP.NET 内容长度:0 }} System.Net.Http.HttpResponseMessage
我有一个类似的 api(只是使用了不同的模型/功能)并且可以正常工作,我已将其用作所有其他人的模板。
只是不明白为什么这是一个错误?
补充:
错误截图
【问题讨论】:
-
错误响应中应该有一些附加信息,您没有提供足够的信息让我们修复它。你确定
_externalApiEmailService.Search有效吗? -
我将再次运行它并再做一次快速观察。那个方法我在它运行任何东西之前设置了一个断点,但我会让它返回一个空值,以防调试器断点不起作用 - 尽管它在我的其他 api 上确实如此