Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

asp.net core - RedirectToAction with parameters in the path

I'm trying to use RedirectToAction to redirect to one of the three actions below. However RedirectToAction is redirecting to /Move/Staging?userId=12345 which results in a 404. What I'm trying to do with the RedirectToAction is that it redirects to /Move/12345/Staging.

I'm using RedirectToAction as follows

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

I've configured the Actions as followed.

    [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;
        }
    }

I've looked into this and as far as I can tell it does work for when you have Move/Staging/{userId}. However, I can't get it to work in my situation with stuff behind the parameter.

question from:https://stackoverflow.com/questions/65884062/redirecttoaction-with-parameters-in-the-path

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks to @King-king I got it working.

attribute routing and convention-based routing are exclusively effective. In this case you use attribute routing so the path used in RedirectToAction won't work.

I modified my actions, for example:

[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;
    }
}

Now I can use RedirectToRoute.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...