未调用 HttpPost 控制器方法

HttpPost controller method not being called

提问人:Luis Angel Urena Lopez 提问时间:10/28/2023 最后编辑:marc_sLuis Angel Urena Lopez 更新时间:10/28/2023 访问量:68

问:

我正在尝试向我所做的控制器方法发出 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")

C# net-core asp.net-core-mvc http-post

评论

0赞 Qiang Fu 10/28/2023
什么是Console.WriteLine(locationResponse.Content.ReadAsStringAsync());
0赞 Luis Angel Urena Lopez 10/28/2023
@QiangFuSystem.Threading.Tasks.Task`1[System.String]
0赞 Luis Angel Urena Lopez 10/28/2023
如果我这样做,我会得到Console.WriteLine(await locationResponse.Content.ReadAsStringAsync());-1
0赞 Qiang Fu 10/28/2023
对不起,一个怎么样Console.WriteLine(response.Content.ReadAsStringAsync().Result)
0赞 Luis Angel Urena Lopez 10/28/2023
@QiangFu-1

答: 暂无答案