来自ASP.NET Core MVC的POST / PUT Web API调用的错误网关 [英] 502 Bad Gateway on POST/PUT Web API Calls from ASP.NET Core MVC

查看:750
本文介绍了来自ASP.NET Core MVC的POST / PUT Web API调用的错误网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用我为应用程序创建的Web API时,我在ASP.NET Core中遇到了奇怪的错误。 GET请求完成并返回它们应该的所有数据,但我的POST / PUT命令都返回502,特别是来自MVC应用程序。我可以从Postman调用API并获得正确的响应,并在数据库中创建对象。

I'm getting strange errors in ASP.NET Core when calling Web API that I have created for the application. GET requests go through fine and return all of the data that they should, but my POST/PUT commands all return a 502, specifically from the MVC application. I can call the API's from Postman and get a proper response and the object is created in the database.


502 - Web服务器在充当
网关或代理服务器时收到无效响应。您找
的页面存在问题,无法显示。当Web服务器(当
充当网关或代理)联系上游内容服务器时,
它从内容服务器收到无效响应。

502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

我正在使用以下代码模拟集成Windows登录,以获取对API的所有Web请求:

I am impersonating an Integrated Windows Login with the following code for all web requests to the API:

async Task Action()
{
    response = await _service.CreateIncident(model);
}

await WindowsIdentity.RunImpersonated(identity.AccessToken, Action);

CreateIncident(型号):

CreateIncident(model):

using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
        {
            var newIncident = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
            var response = await client.PostAsync(hostUri, newIncident);
            return response;
        }

我还通过Ajax创建一个GET请求以获得递增的ID在用户创建新事件之前显示给用户,该事件也会返回502 Bad Gateway。这是一个不正确的IIS设置吗?

There is also one GET Request that I make through Ajax to get an incremented ID to display to the user before they create their new Incident that returns a 502 Bad Gateway as well. Is this an IIS Setting that is incorrect?

推荐答案

如果您使用WindowsIdentity.RunImpersonated和异步函数,它将无法正常工作。在执行非GET请求时,您必须是同步的。我已经更新了我的GitHub问题,我希望能解决这个问题。如果您是此主题的未来访问者,您可以看到最终的位置此处

If you use WindowsIdentity.RunImpersonated and an asynchronous function, it will not work. You must be synchronous when doing non-GET requests. I have updated my GitHub issue, I'm hoping to get this bug addressed. If you are a future visitor to this topic, you can see where this ended up here.

这篇关于来自ASP.NET Core MVC的POST / PUT Web API调用的错误网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆