我正在习惯使用 AspNetCore 2.0。
我有一个 WPF 客户端,它会调用我的 Web API Controller 。
首先这是我的 Web Api(在核心 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, header : { X-SourceFiles: =?UTF-8?B?RDpcREVWLUluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXGFwaVxFbWFpbFJlY29yZFxWYWxpZGF0ZU=tYWls? 日期:2017 年 12 月 10 日星期日 19:58:02 GMT 服务器:红隼 X-Powered-By:ASP.NET 内容长度:0 }} System.Net.Http.HttpResponseMessage
我有一个类似的 api(只是使用了不同的模型/函数)并且工作正常,我已经将其用作所有其他人的模板。
只是不明白为什么这是一个错误?
附加:
错误截图
最佳答案
@人。这个错误的原因是我的 API Controller 构造函数正在接受和接口(interface),但我没有通过我的 StartUp.cs 注入(inject)它,所以,为了说明我对这种不足的耻辱,我将把这个问题留给人们查看!
关于c# - 调用 Web API 时出现 500 Not Found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47742920/