RedirectToAction with parameter

后端 未结 14 1797
傲寒
傲寒 2020-11-22 08:56

I have an action I call from an anchor thusly, Site/Controller/Action/ID where ID is an int.

Later on I need to redirect to th

14条回答
  •  温柔的废话
    2020-11-22 09:51

    The following succeeded with asp.net core 2.1. It may apply elsewhere. The dictionary ControllerBase.ControllerContext.RouteData.Values is directly accessible and writable from within the action method. Perhaps this is the ultimate destination of the data in the other solutions. It also shows where the default routing data comes from.

    [Route("/to/{email?}")]
    public IActionResult ToAction(string email)
    {
        return View("To", email);
    }
    [Route("/from")]
    public IActionResult FromAction()
    {
        ControllerContext.RouteData.Values.Add("email", "mike@myemail.com");
        return RedirectToAction(nameof(ToAction));
             // will redirect to /to/mike@myemail.com
    }
    [Route("/FromAnother/{email?}")]`
    public IActionResult FromAnotherAction(string email)
    {
        return RedirectToAction(nameof(ToAction));
             // will redirect to /to/
             // no need to specify the route part explicitly
    }
    

提交回复
热议问题