路径中带有参数的 RedirectToAction

时间:2021-01-25 11:41:29

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

我正在尝试使用 RedirectToAction 重定向到以下三个操作之一。但是 RedirectToAction 正在重定向到 /Move/Staging?userId=12345 导致 404。我试图用 RedirectToAction 做的是它重定向到 /Move/12345/Staging。

我使用 RedirectToAction 如下

return RedirectToAction("StagingMove", "Maintenance", new { userId = model.userId});

我已按如下方式配置操作。

    [HttpGet("Move/{userId?}/Staging/")]
    public async Task<IActionResult> StagingMove(string userId)
    {
        try
        {
            {Snip}        
            return View(user );
        }
        catch (Exception ex)
        {
            this._logger.LogError(0, ex, "Move User Staging");
            throw ex;
        }
    }

    [HttpGet("Move/{userId?}/Arrival/")]
    public async Task<IActionResult> StagingArrival(string userId)
    {
        try
        {
            ApplicationUser user = await this._userManager.GetUserAsync(userId);        
            return View(user );
        }
        catch (Exception ex)
        {
            this._logger.LogError(0, ex, "Move User Arrival");
            throw ex;
        }
    }

    [HttpGet("Move/{userId?}/Departures/")]
    public async Task<IActionResult> StagingDepartures(string userId)
    {
        try
        {
            ApplicationUser user = await this._userManager.GetUserAsync(userId);        
            return View(user );
        }
        catch (Exception ex)
        {
            this._logger.LogError(0, ex, "Move User Departures");
            throw ex;
        }
    }

我已经研究过这个,据我所知,当你有 Move/Staging/{userId} 时它确实有效。但是,我无法在我的情况下使用参数后面的内容来工作。

1 个答案:

答案 0 :(得分:0)

多亏了 @King-king,我才能成功。

<块引用>

属性路由和基于约定的路由是排他性的 有效的。在这种情况下,您使用属性路由,因此 RedirectToAction 中使用的路径将不起作用。

我修改了我的操作,例如:

[Route("Move/{userId?}/Staging/", Name = "MoveStaging")]
public async Task<IActionResult> StagingMove(string userId)
{
    try
    {
        {Snip}        
        return View(user );
    }
    catch (Exception ex)
    {
        this._logger.LogError(0, ex, "Move User Staging");
        throw ex;
    }
}

现在我可以使用 RedirectToRoute。

return RedirectToRoute("MoveStaging", new { userId = model.userId});