c# - 调用 Web API 时出现 500 Not Found 错误

标签 c# asp.net-core-webapi

我正在习惯使用 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(只是使用了不同的模型/函数)并且工作正常,我已经将其用作所有其他人的模板。

只是不明白为什么这是一个错误?

附加:

错误截图

enter image description here

最佳答案

@人。这个错误的原因是我的 API Controller 构造函数正在接受和接口(interface),但我没有通过我的 StartUp.cs 注入(inject)它,所以,为了说明我对这种不足的耻辱,我将把这个问题留给人们查看!

关于c# - 调用 Web API 时出现 500 Not Found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47742920/

相关文章:

c# - ASP.NET 如何访问 Aplication Request 模块中的 User.Identity.IsAuthenticated?

asp.net-core - 使用 OpenIddict 的授权代码流程和 PKCE 示例

.net-core - VS 2017 如何构建和测试包含 .Net Core 的混合解决方案

c# - 什么是泛型中的 <in T> 和 <out T>

c# - 解释一下这个语句 += () => 的用途以及它是如何工作的

c# - 使用 EmailAddressAttribute 在 .net 中验证电子邮件字符串,但不在属性上

c# - 如何实现 MVC 4 web App 服务器端互斥体

c# - ASP.NET Core API 中的常规路由

c# - 在错误的请求上返回 Json

c# - configuration.getValue 或 configuration.getsection 始终返回 null