【问题标题】:RedirectToAction Throws 502 Bad Gateway [duplicate]RedirectToAction 引发 502 Bad Gateway [重复]
【发布时间】:2017-03-13 19:05:24
【问题描述】:

我有一个 ASP.NET Core MVC 应用程序,它使用集成 Windows 身份验证并调用托管在同一 IIS 服务器上的 Web API(因此对 API 调用使用 WindowsIdentity Impersonation,这也需要身份验证)。大多数路由都有效,但如果执行更新或创建操作并且我尝试将用户重定向到新创建的项目,则会收到 502 Bad Gateway 错误。 POST/PUT 命令通过 Web API 并响应 MVC 应用程序,所以我认为这是 IIS 配置问题,或者路由有问题。

[HttpPost]
public async Task<IActionResult> CreateIncident(Incident model)
{
    HttpResponseMessage response = null;
    var identity = User.Identity as WindowsIdentity;

    async Task Action()
    {
        response = await _service.CreateIncident(model);
    }
    async Task GetId()
    {
        model.IncidentTrackingRefId = await _service.GetNewIncidentId(model.IncidentCategoryLookupTableId,
            model.IncidentTypeLookupTableId);
    }

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

    if (response == null) return RedirectToAction("Error", "Home");

    if (response.StatusCode == HttpStatusCode.Created)
    {
        return RedirectToAction("View", "Incidents", new { id = model.IncidentId });
    }
}

查看操作:

[HttpGet]
public async Task<IActionResult> View(int id)
{
    var identity = User.Identity as WindowsIdentity;

    async Task Action()
    {
        ViewBag.BusTypes = await _service.GenerateDropDown("/GetIncidentBusTypes");
    }

    Incident incident = null;

    async Task GetIncident()
    {
        incident = await _service.GetIncidentById(id);
    }

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

    if (ViewBag.BusTypes == null || incident == null) return RedirectToAction("Error", "Home");
    return View(incident);
}

【问题讨论】:

  • RedirectToAction 导致将 302 响应发送到客户端,并将指向操作集的 URL 路由设置为 Location 标头。这是标准的 HTTP 内容。至此,请求-响应周期完成。但是,通常情况下,客户端随后会针对Location 标头中的 URL 发出新的 GET 请求。看起来您正在尝试重定向到仅使用有效负载响应 POST 的操作。两者都不可能。
  • 我在我的 OP 中添加了 View() 操作。我在我的代码中将它指定为带有属性标记的 HttpGet,所以我认为您关于重定向的观点在这种情况下是不正确的。
  • @RobertMcCoy 您提到的 MVC 应用程序和 WebAPI 应用程序是两个独立的 IIS Web 应用程序?
  • @laika 是的,使用集成 Windows 身份验证和针对 AD 组进行验证以进行授权。 WindowsIdentity.RunImpersonated() 将身份验证发送到 Web API。

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

您不能在 RedirectToAction 中使用model。重定向到操作有这些重载:

RedirectToRouteResult RedirectToAction(string actionName);
RedirectToRouteResult RedirectToAction(string actionName, object routeValues);
RedirectToRouteResult RedirectToAction(string actionName, RouteValueDictionary routeValues);
RedirectToRouteResult RedirectToAction(string actionName, string controllerName);
RedirectToRouteResult RedirectToAction(string actionName, string controllerName, object routeValues);
RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues);

意思是,你不能将整个对象传递给这个方法。

【讨论】:

  • 错误出现在这一行return RedirectToAction("View", "Incidents", new { id = model.IncidentId }); 我不太清楚为什么,但就像它没有正确地将请求转发给操作,因此是 502。该重载遵循 actionName,controllerName , routeValues 细节。
【解决方案2】:

不能使用RedirectToAction 重定向到另一个应用程序

假设你已经以这种方式配置了 MVC 路由

routes.MapRoute(
     "Default",
     "Support/{controller}/action-{action}/{id}",
     new { controller = "Default", action = "Index", id = "" }
);

那么如果你在你的控制器中使用RedirectToAction

return RedirectToAction("View", "Incidents", new { id = model.IncidentId })

浏览器收到此响应

HTTP/1.1 302 Found
Location: http://example.com/Support/Incidents/action-View/123

但是,如果您要重定向到另一个目标应用程序,那么正在处理请求的当前应用程序不知道目标应用程序中的路由表配置是什么 - 它甚至根本不知道它是否使用 MVC。

长话短说,如果您想重定向到另一个应用程序,请使用Redirect

例子:

return Redirect("~/../Application2/Incidents/View");

【讨论】:

  • 但是重定向都发生在 MVC 应用程序中。 Web API 只是遵循 REST API 标准并返回 OK、Created、No Content、Not Found、Bad Request 等正常响应代码,并基于这些响应在 HttpResponseMessage 中引发重定向(如果需要) .
  • @RobertMcCoy Incidents 控制器和 View 操作在 MVC 或 Web API 中?
  • Incidents 控制器在 MVC 应用程序中,并调用 Web API(通过 _services,它只是一个名为 IncidentRESTService 的类来处理所有 API 交互)。然后应根据状态代码或内容解析/处理响应。就像我说的,POST/PUT 请求通过,并且正确的 URL 被放在浏览器的地址栏中,例如在创建一个新事件后,我在 URL 栏中得到https://application/Incidents/View/55,但它抛出一个 502 Bad Gateway错误。我在想这可能只是一个 IIS 错误?
  • 我做了更多的挖掘工作,并浏览了远程服务器上的日志和事件查看器。日志都很好,应用程序查看器没有任何与 502 错误网关直接相关的错误......但它确实有一个似乎随机发生的与 ASP.NET Core 相关的错误:Faulting application name: dotnet.exe, Faulting module name: System.Private.CoreLib.ni.dll
猜你喜欢
  • 2013-08-04
  • 2012-11-26
  • 2018-04-14
  • 2020-04-05
  • 2020-07-12
  • 2023-03-20
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多