加载时,我在
ApiTester
中有一个 HomeController
,将向 POST
中的 API 方法发出 AuthController
请求。
这是我的
ApiTester
课程:
[HttpGet]
public async Task<IActionResult> ApiTester()
{
// Create an HTTP client instance
var client = _httpClientFactory.CreateClient();
// Send a POST request to the AuthController's ApiTest action
var response = await client.PostAsync("https://localhost:7108/api/auth/apitest", null);
if (response.IsSuccessStatusCode)
{
// Read the response content
var result = await response.Content.ReadAsStringAsync();
ViewBag.ApiResponse = result; // Store the result in ViewBag to display on the view
}
else
{
ViewBag.ApiResponse = "Error: " + response.StatusCode;
}
return View(); // Return the view with the response
}
这是
AuthController
中的 API 端点(其中请求甚至未到达):
[HttpPost("ApiTest")]
public IActionResult ApiTest()
{
return Ok(new { message = "200: test successful!" });
}
当我加载此页面并在
var client
处放置断点时,这是我得到的:
在 var 响应中我得到了这个:
var 响应的完整请求消息是:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1,
Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Tue, 08 Oct 2024 05:55:19 GMT
Server: Kestrel
Transfer-Encoding: chunked
Content-Type: application/problem+json; charset=utf-8
}, Trailing Headers:
{
}}
我收到 400 代码。这可能是什么原因造成的?