Asp.net RedirectToAction抛出502坏网关

Asp.net RedirectToAction抛出502坏网关,asp.net,asp.net-mvc,asp.net-core,Asp.net,Asp.net Mvc,Asp.net Core,我有一个ASP.NET核心MVC应用程序,它使用集成的Windows authentication并调用托管在同一IIS服务器上的Web API(因此,对API调用使用WindowsIdentity模拟,这也需要身份验证)。大多数路由都可以工作,但如果执行更新或创建操作,并且我尝试将用户重定向到新创建的项,则会出现502坏网关错误。POST/PUT命令通过Web API并向MVC应用程序提供响应,因此我认为这是IIS配置问题,或者路由有问题 [HttpPost] public async Tas

我有一个ASP.NET核心MVC应用程序,它使用集成的Windows authentication并调用托管在同一IIS服务器上的Web API(因此,对API调用使用WindowsIdentity模拟,这也需要身份验证)。大多数路由都可以工作,但如果执行更新或创建操作,并且我尝试将用户重定向到新创建的项,则会出现502坏网关错误。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 });
    }
}
[HttpPost]
公共异步任务CreateIncident(事件模型)
{
HttpResponseMessage响应=null;
var identity=User.identity作为WindowsIdentity;
异步任务操作()
{
响应=等待服务.CreateIncident(模型);
}
异步任务GetId()
{
model.IncidentTrackingRefId=等待服务.GetNewIncidentId(model.IncidentCategoryLookupTableId,
model.IncidentTypeLookupTableId);
}
等待WindowsIdentity.RunImpersonated(identity.AccessToken,GetId);
等待WindowsIdentity.RunImpersonated(identity.AccessToken,操作);
如果(response==null)返回RedirectToAction(“错误”,“主”);
if(response.StatusCode==HttpStatusCode.Created)
{
返回RedirectToAction(“视图”、“事件”,新的{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);
}
[HttpGet]
公共异步任务视图(int-id)
{
var identity=User.identity作为WindowsIdentity;
异步任务操作()
{
ViewBag.BusTypes=wait _service.GenerateDropDown(“/GetIncidentBusTypes”);
}
事件=空;
异步任务GetIncident()
{
事件=等待服务。GetIncidentById(id);
}
等待WindowsIdentity.RunImpersonated(identity.AccessToken,GetIncident);
等待WindowsIdentity.RunImpersonated(identity.AccessToken,操作);
如果(ViewBag.BusTypes==null | | incident==null)返回RedirectToAction(“错误”,“主”);
返回视图(事件);
}

您不能在重定向操作中使用
模型。重定向到操作具有以下重载:

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

也就是说,您不能将整个对象传递给此方法。

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

假设您以这种方式配置了MVC路由

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

然后,如果在控制器中使用
重定向到操作

return RedirectToAction("View", "Incidents", new { id = model.IncidentId })
浏览器会收到此响应

HTTP/1.1 302 Found
Location: http://example.com/Support/Incidents/action-View/123
但是,如果您重定向到另一个目标应用程序,那么处理请求的当前应用程序不知道目标应用程序中的路由表配置是什么-它甚至不知道是否使用MVC

长话短说,如果要重定向到其他应用程序,请使用

例如:

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

RedirectToAction
导致向客户端发送302响应,并将指向操作的URL路由设置为
位置
头。这是标准的HTTP内容。此时,请求-响应周期就完成了。但是,通常情况下,客户机随后会在
位置
标题中为URL发出新的GET请求。看起来您正试图重定向到一个操作,该操作仅使用有效负载响应POST。这两个都是不可能的。我在我的OP中添加了View()操作。我在代码中将其指定为带有属性标记的HttpGet,因此我认为您关于重定向的观点在本例中不正确。@RobertMcCoy您提到的MVC应用程序和WebAPI应用程序是两个独立的IIS Web应用程序?@laika是,两者都使用集成的Windows身份验证和针对AD组进行验证以获得授权。WindowsIdentity.RunImpersonated()将身份验证发送到Web API。错误出现在以下行
返回重定向到操作(“视图”,“事件”,新{id=model.IncidentId})我不太清楚为什么,但它好像没有正确地将请求转发给操作,因此502。这个重载遵循actionName、controllerName、RouteValue的细节,但重定向都发生在MVC应用程序中。Web API只是遵循REST API标准,返回正常响应代码OK、Created、No Content、Not Found、Bad Request等,并基于这些响应在
HttpResponseMessage
中抛出重定向(如果需要)@RobertMcCoy事件控制器和视图操作在MVC或Web API中?事件控制器在MVC应用程序中,并调用Web API(通过
\u services
,它只是一个名为
IncidentRESTService
的类来处理所有API交互)。然后,应根据状态代码或内容解析/处理响应。正如我所说,POST/PUT请求将通过,并且正确的URL将被放入浏览器的地址栏中,例如,在创建新事件后,我会得到
https://application/Incidents/View/55
,但它会抛出502错误网关。我想这可能只是一个IIS错误?我做了更多的挖掘,并浏览了远程服务器上的日志和事件查看器。日志都正常,应用程序查看器没有任何与502坏网关直接相关的错误……但它确实有一个与ASP.NET Core相关的错误,似乎是随机发生的:
错误应用程序名:dotnet.exe,错误模块名:System.Private.CoreLib.ni.dll