You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HttpPost控制器方法调用异常:返回200但无预期输出

问题:HttpPost控制器请求返回200但无内容,日志未输出却实际触发异常

问题场景

尝试调用自定义的HttpPost控制器方法,请求始终返回200状态码但无任何返回内容,控制器方法开头的Console.WriteLine("here");从未输出。后续排查发现方法实际被调用,但执行过程存在异常,且代码未正确处理返回状态码。

请求发起代码

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;
    }
}

响应信息

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
 }

补充说明

后续确认控制器方法实际被调用,但执行过程中存在异常,且代码未正确处理返回状态码,不过始终无法输出方法开头的Console.WriteLine("here");日志。

内容的提问来源于stack exchange,提问作者Luis Angel Urena Lopez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:22:38