[英]PostAsync with two strings using HttpClient and HttpResponseMessage
[英]Post using HttpClient & Read HttpResponseMessage status
我正在使用HttpClient
發布到API,並獲取HttpResponseMessage
。 我正在從回復中讀取狀態代碼,但我總是200
過帳:
var json = JsonConvert.SerializeObject(loginDto);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var client = new HttpClient();
var response = await client.PostAsync("http://localhost:57770/api/Account/Login", stringContent);
我從API回復HttpResponseMessage
:
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
但是當我閱讀response
,它總是200
我該如何實現?
Asp.Net Core不再將HttpResponseMessage
識別為管道的一部分。 這意味着它將像其他返回的模型一樣對待並序列化為內容。 因此為200 OK狀態。
API控制器操作應返回IActionResult
派生的結果。
[HttpPost]
public IActionResult SomeAction(...) {
//...
return StatusCode((int)HttpStatusCode.Unauthorized); //401
//...
}
或者只是使用
return Unauthorized();
它是從StatusCodeResult
派生的,並被用作簡寫形式來替換上面顯示的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.