提问人:Luis Angel Urena Lopez 提问时间:10/28/2023 最后编辑:marc_sLuis Angel Urena Lopez 更新时间:10/28/2023 访问量:68
未调用 HttpPost 控制器方法
HttpPost controller method not being called
问:
我正在尝试向我所做的控制器方法发出 HTTP Post 请求,但它从未被调用。它始终返回状态代码 200,但未调用且不返回任何内容
这就是我打电话的地方
private async Task louerFilm()
{
LocationRequest locationRequest = new();
locationRequest.IdClient = "99999";
locationRequest.IdFilm = "99999";
var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider;
var token = await customAuthStateProvider.GetToken();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
var locationResponse = await httpClient.PostAsJsonAsync("api/films/louerFilm", locationRequest);
}
这是我的控制器方法
[HttpPost("louerFilm")]
[Authorize]
public int LouerFilm([FromBody] LocationRequest locationRequest)
{
Console.WriteLine("here");
var session = _nHibernateHelper.OpenSession();
try
{
var result = session.CreateSQLQuery("exec pLouerCopieFilm :p1, :p2")
.SetParameter("p1", locationRequest.IdClient)
.SetParameter("p2", locationRequest.IdFilm)
.ExecuteUpdate();
return result;
}
catch (Exception e)
{
Console.WriteLine(e);
return -1;
}
}
从不调用,但响应正文打印如下:Console.WriteLine("here");
locationResponse = StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:
System.Net.Http.BrowserHttpContent, Headers:
{
Date: Sat, 28 Oct 2023 02:51:13 GMT
Server: Kestrel
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
}
编辑:原来它被调用但不正确,我没有正确管理返回的状态代码。无论哪种方式,它都不会输出我在 Controller 中方法开始时添加的内容。Console.WrtieLine("here")
答: 暂无答案
评论
Console.WriteLine(locationResponse.Content.ReadAsStringAsync());
System.Threading.Tasks.Task`1[System.String]
Console.WriteLine(await locationResponse.Content.ReadAsStringAsync());
-1
Console.WriteLine(response.Content.ReadAsStringAsync().Result)
-1