【问题标题】:500 Not Found Error When Calling Web API调用 Web API 时出现 500 Not Found 错误
【发布时间】: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 上确实如此

标签: c# asp.net-core-webapi


【解决方案1】:

500 响应状态代码通常意味着在处理请求时引发了异常(但未处理) - 这可能是从 NullReferenceException 到连接到数据库的错误。

要找出问题所在,您需要捕获异常。我建议阅读Introduction to Error Handling in ASP.NET Core 以获得一些指示,最简单的方法可能是使用开发人员异常页面(该链接的顶部)。

【讨论】:

  • 谢谢,这个链接看起来很好,我会自学的。我会回复 DavidG 并很快回复。
【解决方案2】:

除了@Justin 的回答,我在你的代码中发现了一个错误的路由属性:

[Route("api/[controller]")]
public class EmailRecordController : Controller
{
    // you should add the Part-route-template here:
    [HttpPost("ValidateEmail")]
    // If you leave this, your path is localhost:port/validateemail [Route("ValidateEmail")]
    public EmailRecord ValidateEmail([FromBody] EmailRecord emailRecord)
    {
        return _externalApiEmailService.Search(emailRecord.email);
    }
}

【讨论】:

  • 谢谢。我已经把它放进去,但仍然得到同样的错误
【解决方案3】:

@人。这个错误的原因是我的 API 控制器构造函数正在接受和接口,但我没有通过我的 StartUp.cs 注入它,所以,为了说明我在这样一个不足之处的耻辱,我将把这个问题留给人们查看!

【讨论】:

  • 你救了我,兄弟..我盲目地创建核心 web api,牢记 web api..你睁开了眼睛......我必须更加小心
  • 你是如何解决这个问题的?我也有同样的问题。但我已经在 ninject 中注册了我的接口和类
猜你喜欢
  • 1970-01-01
  • 2017-07-04
  • 2021-01-12
  • 2016-10-05
  • 2019-01-28
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多