Skip to content

implement WhereAmIController#3

Closed
js00000000 wants to merge 5 commits intokyoangel:masterfrom
js00000000:master
Closed

implement WhereAmIController#3
js00000000 wants to merge 5 commits intokyoangel:masterfrom
js00000000:master

Conversation

@js00000000
Copy link
Copy Markdown

implement WhereAmIController

Comment on lines +37 to +46
var response = new HttpResponseMessage();
try
{
response = await _client.PostAsJsonAsync("http://ip-api.com/batch", ipList);
return await response.Content.ReadAsAsync<List<IpDetail>>();
}
catch (Exception e)
{
throw new Exception($"Status Code: {response.StatusCode}, Error message: {e}");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這邊你先宣告 response 看起來是為了在exception的時候紀錄status code
其實你這邊的寫法是接了exception又補充資料 丟出exception.
但我覺得有更好的做法 就是讓例外直接丟,然後透過Action filter去外面寫下response的結果
aspnetcore有exceptionfilter可以寫

var response = await _WhereAmIService.GetIp();
var request = PrepareGetDetail(response.Ip);
var detail = await _WhereAmIService.GetIpDetail(request);
return $"Ip: {detail[0].Query}, Country Code: {detail[0].CountryCode}";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你這邊回的是字串,而不是json, 可以直接回actionResult。可以試著用Anonymous type. 或定義回傳物件

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想這就是為什麼你有httpclientFactory.Create()的靜態方法可以用的原因!

@kyoangel kyoangel closed this Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants