1

Forgive me for my ignorance, I am new to writing and consuming ASP.Net Core web APIs.

I have a web project that has an MVC architecture. The controllers call a web api to get data.

This is what I have so far:

using (var response = await _httpClient.GetAsync($"{_apiSettings.Value.BaseURL}/authors/{id}"))
{
    if (response.IsSuccessStatusCode)
    {
        var apiResponse = await response.Content.ReadAsStringAsync();
        var authorData = JsonSerializer.Deserialize<AuthorDTO>(apiResponse);
        authorForm = _mapper.Map<AuthorViewModel>(authorData);
    }
    else
    {
        // What do I do here?
    }
}

// more stuff down here to get to view...

As you can see I am not sure what should happen when I don't get a 200 code from the API call.

[Edit]

Is there a way to pass on the same HTTP response code? Is this a bad idea because that info shouldn't get to the user?

Is it best to just throw an error? If so should it always just be an Internal Server error?

I am just looking for advice on best practices, I realize there is no one right answer here.

5
  • You could throw an exception, and let your global error handling catch it. Or you could display a different view with a suitable error message. (Eg: 404 returns an "Author not found" view.) There's no one right answer to this. Commented Jan 13, 2022 at 15:26
  • @RichardDeeming I figured there was no right answer, I will add some to my question to be more specific Commented Jan 13, 2022 at 15:27
  • I think you can log the time, url, status code, response content and other info can help you. Commented Jan 13, 2022 at 15:30
  • @player2135 That sounds like a great option, could you put that in an answer showing how to do so with a small example? Commented Jan 13, 2022 at 15:32
  • There are many ways to pass HTTP response status, you can refer to this post: stackoverflow.com/questions/10655350/… Commented Jan 14, 2022 at 3:09

2 Answers 2

2

I think you can log the time, url, status code, response content and other info can help you. A Example From My Project

private static async Task<T> HandleHttpResponse<T>(HttpResponseMessage response)
{
    if (response.IsSuccessStatusCode)
    {
        var result = await response.Content.ReadAsStringAsync();
        return JsonHelper.ToObject<T>(HandleHttpResult(result));
    }
    else
    {
        var result = await response.Content.ReadAsStringAsync();
        LogHelper.WriteLog($"请求{response.RequestMessage.RequestUri}错误,错误描述:{response.StatusCode},错误码:{response.StatusCode.ToInt32()},错误内容:{result}", $"{nameof(RemoteServerHelper)}_{nameof(HandleHttpResponse)}", true);
    }
    return default;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Well, there are several ways to return a request.

Example:

  • System.Net.HttpStatusCode.Ok() (return status 200)
  • System.Net.HttpStatusCode.BadRequest() (return status 400)

Here is the microsoft documentation:

https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.statuscode?view=net-6.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.