简体   繁体   中英

500 Not Found Error When Calling Web API

I am getting used to using AspNetCore 2.0.

I have a WPF client and it invokes my web api controller.

First this is my Web Api (under core 2.0).

[Route("api/[controller]")]
public class EmailRecordController : Controller
{
    [HttpPost]
    [Route("ValidateEmail")]
    public EmailRecord ValidateEmail([FromBody] EmailRecord emailRecord)
    {
        return _externalApiEmailService.Search(emailRecord.email);
    }
}

I call it from my WPF Client:

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);
    }
}

My Model on my Client is:

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; }
}

My model on my asp.net.core server is:

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; }
}

When it is invoked I get this:

  • response {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-SourceFiles: =?UTF-8?B?RDpcREVWLUluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXEluZm9ybWVkV29ya2VyXGFwaVxFbWFpbFJlY29yZFxWYWxpZGF0ZUVtYWls?= Date: Sun, 10 Dec 2017 19:58:02 GMT Server: Kestrel X-Powered-By: ASP.NET Content-Length: 0 }} System.Net.Http.HttpResponseMessage

I have a similar api (just using a different model/function) and that works Ok and I have used that as a template for all others.

Just cannot see why this is an error?

Additional:

Screenshot of error

在此处输入图片说明

A 500 response status code normally means that an exception was thrown (and not handled) while processing the request - that could be anything from a NullReferenceException to an error connecting to a database.

To figure out what the issue is you need to capture the exception. I suggest reading Introduction to Error Handling in ASP.NET Core for some pointers, the simplest way will probably be to use the developer exception page (top section in that link).

In addition to @Justin 's answer, I found an false routing attribute in your Code:

[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);
    }
}

@People. the reason this erred was that my API controller constructor was accepting and interface but I have not injected it via my StartUp.cs, So, to illustrate my shame in such a short-falling I shall leave this question up for people to view it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM